当使用自定义键盘时,keyboardWillShow
运行两次(正常行为)第一个高度为0但第二个是我的情况667中的正确高度。问题是这只是第二次viewController显示。我第一次得到下面的奇怪输出。
控制台:
keyboardSize CGRect(origin =(x = 0,y = 258),size =(width = 0,height = 2.8876618518302306E-314))
第二次打开视图控制器时控制台:
keyboardSize CGRect(origin =(x = 0,y = 0),size =(width = 0,height = 667))
我的代码:
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillShow:"), name: UIKeyboardWillShowNotification, object: nil)
func keyboardWillShow(notification: NSNotification) {
if let userInfo = notification.userInfo {
if let keyboardSize = (userInfo[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue() {
if keyboardSize.height > 0 { //in case of custom keyborad
kbHeight = keyboardSize.height
self.animateTextField(true)
}
}
}
}
答案 0 :(得分:3)
将UIKeyboardFrameBeginUserInfoKey
更改为UIKeyboardFrameEndUserInfoKey
。就是这样:
func keyboardWillShow(notification: NSNotification) {
if let userInfo = notification.userInfo {
if let keyboardSize = (userInfo[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.CGRectValue() {
if keyboardSize.height > 0 { //in case of custom keyborad
kbHeight = keyboardSize.height
self.animateTextField(true)
}
}
}
}
继续编码..............:)