码迷,mamicode.com
首页 > 移动开发 > 详细

ios 监控键盘状态

时间:2019-12-07 10:54:23      阅读:150      评论:0      收藏:0      [点我收藏+]

标签:hdu   Once   default   hide   cat   lsh   efault   remove   hid   

增加键盘显示和隐藏事件监听

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

  删除事件监听

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

  

ios 监控键盘状态

标签:hdu   Once   default   hide   cat   lsh   efault   remove   hid   

原文地址:https://www.cnblogs.com/rchao/p/12000606.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!