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

问题描述 投票:2回答:1

在使用自定义键盘,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)
                }
            }
        }
    }   
ios swift
1个回答
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)
                }
            }
        }
    }  

保持编码.............. :)

© www.soinside.com 2019 - 2024. All rights reserved.