swift 解决键盘遮挡

发布时间:2020-04-23 22:57:44

 //监听键盘通知

        NotificationCenter.default.addObserver(self,

                                               selector: #selector(keyboardWillChange(_ :)),

                                               name: UIResponder.keyboardWillChangeFrameNotification, object: nil)



 //键盘改变

    @objc func keyboardWillChange(_ notification:Notification) {

        DispatchQueue.main.async {

            if let userInfo = notification.userInfo,

                let value = userInfo[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue,

                let duration = userInfo[UIResponder.keyboardAnimationDurationUserInfoKey] as? Double,

                let curve = userInfo[UIResponder.keyboardAnimationCurveUserInfoKey] as? UInt {

                

                let frame = value.cgRectValue

                let intersection = frame.intersection(self.view.frame)

                self.footView.snp.updateConstraints { (make) in

                    make.bottom.equalToSuperview().offset(-intersection.height);

                }

                UIView.animate(withDuration: duration, delay: 0.0,

                               options: UIView.AnimationOptions(rawValue: curve),

                               animations: {

                                self.view.layoutIfNeeded()

                }, completion: nil)

            }

        }

    }



Card image cap
APP STORE
Card image cap
应用宝
Card image cap
小米
Card image cap
华为