美文网首页
iOS键盘通知使用

iOS键盘通知使用

作者: _风雨 | 来源:发表于2021-08-13 16:06 被阅读0次

    Swift 5

    今天用change notification的时候,出现键盘始终不收起的bug,最后还是采用了 Show, Hide的方式, 特此记录一下

    func registeNotifications() {
            NotificationCenter.default.addObserver(self,
                                                   selector: #selector(keyboardWillShow(_:)),
                                                   name: UIResponder.keyboardWillShowNotification, object: nil)
            NotificationCenter.default.addObserver(self,
                                                   selector: #selector(keyboardWillHide(_:)),
                                                   name: UIResponder.keyboardWillHideNotification, object: nil)
            // change 关闭键盘,发现iOS 14.7上不收起键盘,原因未知
    //        NotificationCenter.default.addObserver(self, selector: #selector(keyboardFrameChanged(_:)), name: UIResponder.keyboardWillChangeFrameNotification, object: nil)
        }
    
     @objc func keyboardWillShow(_ noti: Notification) {
            guard let _ = noti.userInfo else {
                return
            }
            
            let duration = noti.keyboardAnimationDuration()
            
            let cureAnimation = noti.keyboardAnimationCurve()
            
            let keyboardHeight = noti.keyboardHeight()
            
            inputBar.snp.updateConstraints { make in
                make.bottom.equalTo(self.view.snp.bottom).offset(-keyboardHeight)
            }
            
            UIView.animate(withDuration: duration, delay: 0.0, options: cureAnimation) {
                self.view.layoutIfNeeded()
            } completion: { _ in
                
            }
        }
        
        @objc func keyboardWillHide(_ noti: Notification) {
            guard let _ = noti.userInfo else {
                return
            }
            let duration = noti.keyboardAnimationDuration()
            
            let cureAnimation = noti.keyboardAnimationCurve()
            
            inputBar.snp.updateConstraints { make in
                make.bottom.equalTo(self.view.snp.bottom).offset(0)
            }
            
            UIView.animate(withDuration: duration, delay: 0.0, options: cureAnimation) {
                self.view.layoutIfNeeded()
            } completion: { _ in
                
            }
        }
    

    Notification extension

    extension Notification {
        func keyboardHeight() -> CGFloat {
            if let userInfo = self.userInfo, let value = userInfo[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue {
                let size = value.cgRectValue.size
                return size.height
            }
            return 0
        }
        
        func keyboardAnimationCurve() -> UIView.AnimationOptions {
            if let userInfo = self.userInfo {
                if let curveValue = (userInfo[UIResponder.keyboardAnimationCurveUserInfoKey] as? NSNumber)?.uintValue {
                    return UIView.AnimationOptions(rawValue: curveValue << 16)
                }
            }
            return UIView.AnimationOptions.curveEaseInOut
        }
        
        func keyboardAnimationDuration() -> Double {
            if let userInfo = self.userInfo {
                if let duration = userInfo[UIResponder.keyboardAnimationDurationUserInfoKey] as? Double {
                    return duration
                }
            }
            return 0.25
        }
    }
    

    相关文章

      网友评论

          本文标题:iOS键盘通知使用

          本文链接:https://www.haomeiwen.com/subject/sotjbltx.html