ios keyboard status monitoring

Add keyboard display and hide the event listeners

NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(notification:)), name: UIResponder.keyboardWillShowNotification, object: nil)
        
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(notification:)), name: UIResponder.keyboardWillHideNotification, object: nil)

 

@objc func keyboardWillShow(notification:Notification){
        let userinfo:Dictionary = notification.userInfo!
        let frame = (userinfo[UIResponder.keyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
        UIView.animate(withDuration: 0.4, animations: {
            self.view.frame.origin.y = 0 - frame.height
        })
    }
    
    @objc func keyboardWillHide(notification:Notification){
        UIView.animate(withDuration: 0.4, animations: {
            self.view.frame.origin.y = 0
        })
    }

  Delete an event listener

NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardWillShowNotification, object: nil)
        NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardWillHideNotification, object: nil)

  

Guess you like

Origin www.cnblogs.com/rchao/p/12000606.html