gpt4 book ai didi

ios - 将 ScrollView 移动到原始状态

转载 作者:行者123 更新时间:2023-12-01 19:54:54 24 4
gpt4 key购买 nike

我想在键盘出现时向上移动 View ,并在键盘消失时将 View 向后移动。我为此使用 ScrollView 。

我有这个几乎工作。我的错误是这样的:

  • 你触发键盘显示
  • 你触发键盘隐藏

  • 到目前为止,事情进展顺利。
  • 你触发键盘显示
  • 您触发键盘隐藏 <-- 这不再起作用了,但第一次它可以完美运行。

  • 键盘出现时的代码(超时工作):
    func keyboardWasShown(notification: NSNotification) {
    var userInfo = notification.userInfo!
    let keyboardFrame:CGRect = (userInfo[UIKeyboardFrameBeginUserInfoKey] as! NSValue).cgRectValue
    let buttonOrigin: CGPoint = btn.frame.origin
    let buttonHeight: CGFloat = btn.frame.size.height
    var visibleRect: CGRect = view.frame

    visibleRect.size.height -= keyboardFrame.size.height
    if !visibleRect.contains(buttonOrigin) {
    let scrollPoint = CGPoint(x: CGFloat(0.0), y: CGFloat(buttonOrigin.y - visibleRect.size.height + (buttonHeight + 8)))
    scrollView.setContentOffset(scrollPoint, animated: true)
    }
    }

    键盘要隐藏时的代码(此代码第一次有效,第二次无效):
    func keyboardWillBeHidden(notification: NSNotification){
    var info = notification.userInfo!
    let keyboardSize = (info[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue.size

    let contentInsets : UIEdgeInsets = UIEdgeInsetsMake(0.0, 0.0, -keyboardSize!.height, 0.0)

    self.scrollView.contentInset = contentInsets
    self.scrollView.scrollIndicatorInsets = contentInsets
    self.view.endEditing(true)
    self.scrollView.isScrollEnabled = false
    }

    这就是我的观点:
    enter image description here

    当键盘显示时,一切都需要向上移动。上移作品:

    enter image description here

    但是当键盘消失时它不起作用:
    enter image description here

    最佳答案

    只需更改此行
    let contentInsets : UIEdgeInsets = UIEdgeInsetsMake(0.0, 0.0, -keyboardSize!.height, 0.0)

    let contentInsets : UIEdgeInsets = .zero
    或试试这个

    //MARK:- 键盘方法

      func keyboardWillOpen(sender:Notification) {

    if let info = sender.userInfo{

    if let keyboardSize = info[UIKeyboardFrameBeginUserInfoKey] as? CGRect{
    let contentInsets = UIEdgeInsetsMake(0, 0, -keyboardSize.height, 0)


    self.scrollView.contentInset = contentInsets
    self.scrollView.scrollIndicatorInsets = contentInsets
    }
    }
    }

    func keyboardWillHide(notification:Notification) {

    if let info = notification.userInfo {

    if let keyboardSize = info[UIKeyboardFrameBeginUserInfoKey] as? CGRect {

    self.scrollView.contentInset = .zero
    self.scrollView.scrollIndicatorInsets = .zero

    }
    }
    }

    关于ios - 将 ScrollView 移动到原始状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43158278/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com