具有底部约束的键盘处理 [英] Handling keyboard with bottom constraint

查看:53
本文介绍了具有底部约束的键盘处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用常规设计和聊天应用程序的UI构建聊天应用程序.

I'm building a chat app with a regular design and UI of a chat app.

我有一个我的应用需要的工具栏"(0,0 375x66),我希望它在显示键盘时保持在原来的位置.它具有4个约束:顶部:0,前导:0,尾部:0,长宽比:125:22

I have a "toolbar" that my app needs (0,0 375x66) and I want it to stay where it is when the keyboard is shown. It has 4 constraints: top: 0, leading: 0, trailing: 0, aspect-ratio: 125:22

我有一个 UICollectionView (0,66 375x530),我希望在显示键盘时像其他任何聊天应用一样进行滚动.它具有4个约束:顶部(至工具栏):0,前导:0,尾部:0,底部(至newMessageView,我将在稍后说明):0

I have a UICollectionView (0,66 375x530) that I want to be scrolled like any other chat app when the keyboard is shown. It has 4 constraints: top (to the toolbar): 0, leading: 0, trailing: 0, bottom (to the newMessageView I'll explain shortly): 0

我有一个 UIView ,其中包含一个 UITextField .我们称之为 newMessageView (0,596 375x71),它有4个约束:底部:0,前导:0,尾部:0,宽高比:375:71

I have a UIView that has a UITextField inside it. let's call it newMessageView (0,596 375x71) and it has 4 constraints: bottom: 0, leading: 0, trailing: 0, aspect-ratio: 375:71

现在,我正在尝试在显示键盘时带来 newMessageView 应用程序.我正在尝试这样:

Now, I'm trying to bring the newMessageView app when the keyboard is shown. I'm trying to to it like this:

@objc func keyboardWillShow(notification: NSNotification) {
    if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
        if self.view.frame.origin.y == 0{
            self.newMessageViewBottomConstraint.constant += keyboardSize.height
        }
    }
}

@objc func keyboardWillHide(notification: NSNotification) {
    if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
        if self.view.frame.origin.y != 0{
            self.newMessageViewBottomConstraint.constant -= keyboardSize.height
        }
    }
}

,但完全无法正常工作.视图跳来躲去,我真的不明白为什么.

but it completly doesn't work. The view jumps and hides and I really can't understand why.

我做对了吗?有人可以帮助我并指导我吗?

Am I doing it right? Can anyone help me and guide me with this?

推荐答案

在更新约束后必须使用 self.view.layoutIfNeeded()

You have to use self.view.layoutIfNeeded() after Update constraint

我为您编写了完整的解决方案

i write complete solution for you

 class ViewController: UIViewController {

        override func viewDidLoad() {
            super.viewDidLoad()

            NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillShow), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
            NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillHide), name: NSNotification.Name.UIKeyboardWillHide, object: nil)

        }

        @objc func keyboardWillShow(notification: Notification) {
            self.keyboardControl(notification, isShowing: true)
        }

        @objc func keyboardWillHide(notification: Notification) {
            self.keyboardControl(notification, isShowing: false)
        }


        private func keyboardControl(_ notification: Notification, isShowing: Bool) {


            /* Handle the Keyboard property of Default*/

            var userInfo = notification.userInfo!
            let keyboardRect = (userInfo[UIKeyboardFrameEndUserInfoKey]! as AnyObject).cgRectValue
            let curve = (userInfo[UIKeyboardAnimationCurveUserInfoKey]! as AnyObject).uint32Value

            let convertedFrame = self.view.convert(keyboardRect!, from: nil)
            let heightOffset = self.view.bounds.size.height - convertedFrame.origin.y
            let options = UIViewAnimationOptions(rawValue: UInt(curve!) << 16 | UIViewAnimationOptions.beginFromCurrentState.rawValue)
            let duration = (userInfo[UIKeyboardAnimationDurationUserInfoKey]! as AnyObject).doubleValue



            var  pureheightOffset : CGFloat = -heightOffset

            if isShowing { /// Wite space of save area in iphonex ios 11
                if #available(iOS 11.0, *) {
                    pureheightOffset = pureheightOffset + view.safeAreaInsets.bottom
                }
            }

            // Here change you Consrant
         //   self.actionBarPaddingBottomConstranit?.update(offset:pureheightOffset)

            UIView.animate(
                withDuration: duration!,
                delay: 0,
                options: options,
                animations: {
                    self.view.layoutIfNeeded()
            },
                completion: { bool in

            })

        }

这篇关于具有底部约束的键盘处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆