Swift--监听iPhone键盘弹出及隐藏事件
2017-12-11 14:05
501 查看
开发需求:对键盘弹出及隐藏事件进行监听
需要通过NotificationCenter对键盘事件进行监听//键盘即将弹出 NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardShow(note:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil) //键盘即将隐藏 NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardHidden(note:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil) //键盘弹出监听 @objc func keyboardShow(note: Notification) { guard let userInfo = note.userInfo else {return} guard let keyboardRect = userInfo[UIKeyboardFrameEndUserInfoKey] as? CGRect else{return} //获取键盘弹起的高度 let keyboardTopYPosition = SCREENHEIGHT - keyboardRect.height } //键盘隐藏监听 @objc func keyboardHidden(note: Notification){ } //取消键盘监听 deinit { NotificationCenter.default.removeObserver(self) }
如果要监听键盘完全弹起或隐藏后进行操作,则使用
//注意,这里使用的是UIKeyboardDidShow NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardShow(note:)), name: NSNotification.Name.UIKeyboardDidShow, object: nil) //注意,这里使用的是UIKeyboardDidHide NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardHidden(note:)), name: NSNotification.Name.UIKeyboardDidHide, object: nil)
相关文章推荐
- Android监听系统输入法键盘弹出显示与隐藏事件
- android 监听软键盘的弹出与隐藏
- 安卓监听软键盘显示和隐藏事件
- Andorid 监听软键盘的隐藏与弹出
- Android 监听软键盘 弹出和隐藏 (通过一个类解决)
- Android 键盘使用一(监听弹出隐藏)
- 关闭键盘及监听越狱设备隐藏键盘的事件
- Android监听软键盘弹出与隐藏的两种方法
- swift 监听键盘弹出的高度
- 监听iPad键盘显示和隐藏事件
- 监听键盘的弹出和隐藏
- swift:iOS模拟器键盘设置(弹出,中文键盘,隐藏)
- android Edittext 监听软键盘弹出显示和消失隐藏
- 软键盘的弹出隐藏监听及软键盘高度
- 监听键盘弹出 隐藏
- iOS swift-UIView随键盘弹出/隐藏而改变位置
- EditText 监听、焦点事件、自定义样式全解及隐藏键盘
- 安卓监听键盘弹出/隐藏
- android学习,通过监听焦点,来进行类似监听软键盘弹出隐藏的功能
- android监听键盘弹出or隐藏方法