我正在尝试使用键盘扩展从 userInfo
获取键盘大小。
我加
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillShow:", name: UIKeyboardDidShowNotification, object: nil)
在我的 KeyboardViewController.swift 中,但是 keyboardWillShow
永远不会被调用。
最佳答案
先注册键盘通知
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
let notificationCenter = NSNotificationCenter.defaultCenter()
notificationCenter.addObserver(self, selector: "keyboardWillShow:", name: UIKeyboardWillShowNotification, object: nil)
notificationCenter.addObserver(self, selector: "keyboardWillHide:", name: UIKeyboardWillHideNotification, object: nil)
}
override func viewDidDisappear(animated: Bool) {
super.viewDidDisappear(animated)
let notificationCenter = NSNotificationCenter.defaultCenter()
notificationCenter.removeObserver(self, name: UIKeyboardWillShowNotification, object: nil)
notificationCenter.removeObserver(self, name: UIKeyboardWillHideNotification, object: nil)
}
然后
func keyboardWillShow(notification: NSNotification) {
let userInfo = notification.userInfo!
let keyboardHeight = (userInfo[UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue().height
}
并且您将高度值作为 CGFloat
关于ios - 键盘通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28129041/