微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

为什么我在底部视图和键盘之间获得额外空间而键盘快速向上移动

如何解决为什么我在底部视图和键盘之间获得额外空间而键盘快速向上移动

对于我已经给出的底视图

 bottom = leading  = trialing = 0,height = 100

并且我已经给了底部视图 NSLayoutConstraint 以在键盘出现时向上移动

 @IBOutlet weak var viewbottomConstraint: NSLayoutConstraint!

代码

super.viewDidLoad()
    messageTextfield.delegate = self
    NotificationCenter.default.addobserver(self,selector: #selector(handleKeyboardNotification),name: UIResponder.keyboardWillShowNotification,object: nil)
    
    NotificationCenter.default.addobserver(self,name: UIResponder.keyboardWillHideNotification,object: nil)
 
}

    @objc func handleKeyboardNotification(_ notification: Notification) {

    if let userInfo = notification.userInfo {
        
        let keyboardFrame = (userInfo[UIResponder.keyboardFrameEndUserInfoKey] as AnyObject).cgRectValue
        
        let isKeyboardShowing = notification.name == UIResponder.keyboardWillShowNotification
        viewbottomConstraint?.constant = isKeyboardShowing ? keyboardFrame!.height : 0

        UIView.animate(withDuration: 0.5,animations: { () -> Void in
            self.view.layoutIfNeeded()

        })
    }
}

override func viewWilldisappear(_ animated: Bool) {
    NotificationCenter.default.removeObserver(self)
}
override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    self.bottomContainerView.superview?.setNeedsLayout()
}

然后 o/p 如下所示:gap b/w keyboard and bottomview

OP img

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。