【问题标题】:How to move the view Y axis change when the keyboard appears键盘出现时如何移动视图Y轴变化
【发布时间】:2021-01-31 07:48:26
【问题描述】:

我正在使用以下功能,其中一个功能是在键盘出现时更改 Y 轴,另一个功能是,如果您点击视图上的任意位置,键盘就会消失并且框架应该回到原来的位置。

我正在使用 firestore,因此当我启动应用程序时,我正在使用 firestore Auth 来检查用户是否已登录,如果用户未登录,则会显示登录页面,一旦您登录,登录页面是被解雇,你回到根控制器,你会看到一个标签,其中包含三个标签,每个标签连接到一个单独的视图控制器,每个标签都放入一个导航控制器。

第一个选项卡向您展示了一个视图控制器,其中的表格视图显示了我的所有联系人, 然后我点击一个单元格,将我带到我的消息视图控制器,在那里我可以发送和接收消息。

在 viewWillAppear 的消息视图控制器中,我将标签栏 isHidden 设置为 true,然后将 UITextView 设置为视图控制器的底部

应该发生的是,一旦您点击 UITextView,UITextView 应该会随着键盘的出现而向上移动,

我使用相同的功能,但发生的是键盘出现并覆盖了UITextView,

然后,当我点击主视图以关闭键盘时,键盘消失了,但框架随后向上移动,但它如何向上移动到一定距离也没有任何意义,因为它没有向上移动到应有的位置'那时我一直在想,也许它正在移动到标签栏的高度,但没有这样做,它介于两者之间

让我难以理解的是,我还尝试过,当您点击联系人单元格而不是将消息视图控制器推送到我使用的当前导航堆栈时,当我这样做时它工作得非常好,但我会真的很喜欢消息控制器在导航堆栈中,所以我可以轻松地向后移动

任何反馈将不胜感激

    func keyboardNotifications() {
        
        NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillShow), name: UIResponder.keyboardWillShowNotification, object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide), name: UIResponder.keyboardWillHideNotification, object: nil)
    }
    
    
    @objc func keyboardWillShow(notification: NSNotification) {
        if let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
            if self.view.frame.origin.y == 0 {
                self.view.frame.origin.y -= keyboardSize.height
            }
        }
    }
    
    
    @objc func keyboardWillHide(notification: NSNotification) {
        if self.view.frame.origin.y != 0 {
            self.view.frame.origin.y = 0
        }
    }
     

【问题讨论】:

    标签: swift keyboard uitextfield nsnotificationcenter


    【解决方案1】:

    您可以使用以下代码。 注意:bottomConstraint 是包含 textview 的视图的底部约束

     override func viewDidLoad() {
            super.viewDidLoad()
            
           . 
           . 
           .
            NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: UIResponder.keyboardWillShowNotification, object: nil)
            NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide), name: UIResponder.keyboardWillHideNotification, object: nil)
            NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillChangeFrame), name: UIResponder.keyboardWillChangeFrameNotification, object: nil)
        }
        
        
        @objc func keyboardWillShow(_ notification:Notification) {
            adjustingHeight(true, notification: notification)
        }
        
        @objc func keyboardWillHide(_ notification:Notification) {
            adjustingHeight(false, notification: notification)
        }
        
        @objc func keyboardWillChangeFrame(_ notification: Notification) {
            adjustingHeight(false, notification: notification)
        }
        
        func adjustingHeight(_ show:Bool, notification:Notification) {
            
            var userInfo = (notification as NSNotification).userInfo!
            let keyboardFrame:CGRect = (userInfo[UIResponder.keyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
            let animationDurarion = userInfo[UIResponder.keyboardAnimationDurationUserInfoKey] as! TimeInterval
            let changeInHeight = (keyboardFrame.height ) * (show ? 1 : 0)
            self.bottomConstraint.constant = changeInHeight
            
            UIView.animate(withDuration: animationDurarion, animations: { () -> Void in
                self.view.layoutIfNeeded()
            })
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-12-19
      • 2018-05-21
      • 2016-05-07
      • 1970-01-01
      • 2015-11-29
      • 2012-07-25
      • 2023-03-08
      相关资源
      最近更新 更多