keyboardWillShow第一次显示一个奇怪的高度

时间:2015-08-10 15:48:48

标签: ios swift

当使用自定义键盘时,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)
                }
            }
        }
    }   

1 个答案:

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

继续编码..............:)