【问题标题】:Handling keyboard with bottom constraint处理具有底部约束的键盘
【发布时间】:2023-03-10 21:43:01
【问题描述】:

我正在构建一个具有常规设计和聊天应用 UI 的聊天应用。

我有一个我的应用程序需要的“工具栏”(0,0 375x66),我希望它在显示键盘时保持原位。它有 4 个约束:顶部:0,前导:0,尾随:0,纵横比:125:22

我有一个UICollectionView (0,66 375x530),我想在显示键盘时像任何其他聊天应用程序一样滚动它。它有 4 个约束:顶部(到工具栏):0,前导:0,尾随:0,底部(到 newMessageView,我稍后会解释):0

我有一个UIView,里面有一个UITextField。我们称它为newMessageView (0,596 375x71),它有 4 个约束:底部:0,前导:0,尾随:0,纵横比:375:71

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

@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
        }
    }
}

但它完全不起作用。视图跳跃和隐藏,我真的不明白为什么。

我做得对吗?任何人都可以帮助我并指导我吗?

【问题讨论】:

  • 你能分享故事板视图层次的截图吗?我认为您的文本视图隐藏在其他视图后面。
  • 您是否在控制台上看到任何与约束相关的错误?
  • @ParthBhuva 没有。一切看起来都很好,没有错误
  • 在 xcode 中尝试查看调试器以找出打开键盘后您的视图在哪里

标签: ios iphone swift cocoa-touch uiview


【解决方案1】:

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

我为你写完整的解决方案

 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

            })

        }

【讨论】:

  • 为了防止观察者悬空,我建议在视图控制器中添加一个 deinit {} 例程:NotificationCenter.default.removeObserver(self)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-25
  • 1970-01-01
相关资源
最近更新 更多