keyboardWillShow第一次显示奇怪的高度

Mik*_*ika 2 ios swift

使用自定义键盘时,keyboardWillShow运行两次(正常行为),第一个高度为0,但第二个在我的情况下为正确的高度667。问题是,这仅在第二次显示viewController时才是正确的。我第一次得到下面的奇怪输出。

第一次打开视图控制器时进行控制台:

keyboardSize CGRect(来源=(x = 0,y = 258),大小=(宽度= 0,高度= 2.8876618518302306E-314))

第二次打开视图控制器时进行控制台:

keyboardSize CGRect(来源=(x = 0,y = 0),大小=(宽度= 0,高度= 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)
                }
            }
        }
    }   
Run Code Online (Sandbox Code Playgroud)

Kri*_*lim 5

改变UIKeyboardFrameBeginUserInfoKeyUIKeyboardFrameEndUserInfoKey。就这样:

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)
                }
            }
        }
    }  
Run Code Online (Sandbox Code Playgroud)

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

  • 谢谢....今天我自己在 UIKeyboardFrameBeginUserInfoKey 上浪费了很多时间。 (2认同)