键盘将显示两次

mih*_*tel 5 keyboard-events ios swift

我遇到了问题“keyboardWillShow”触发两次,但“keyboardWillHide”调用一次。

这是一个示例,其中我在“keyboardWillShow”触发后立即打印键盘尺寸。我还在“viewDidLoad”中放置了断点,并且观察者仅注册一次。我添加了两个元素“UITextField”和“UITextView”,两者的行为相同。

我正在使用 iOS 9.2、swift 语言、xcode 7

在我的 ViewController 下面

class ViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillShow:"), name: UIKeyboardWillShowNotification, object: nil)
    NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillHide:"), name: UIKeyboardWillHideNotification, object: nil)
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func keyboardWillShow(notification: NSNotification) {
    if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue() {
        print("keyboardWillShow sizes: \(keyboardSize)")
    }

}

func keyboardWillHide(notification: NSNotification) {
    print("HideHideHide")
}

}
Run Code Online (Sandbox Code Playgroud)

更新

第一次触发一次,尺寸为:keyboardWillShow 尺寸:(0.0, 568.0, 320.0, 253.0 )

其余的两次,尺寸不同:(第二个 y 位置发生变化,高度也发生变化) KeyboardWillShow 尺寸: (0.0, 568.0, 320.0, 216.0 ) KeyboardWillShow 尺寸: (0.0, 352.0 , 320.0, 216.0 )

mih*_*tel 1

问题与模拟器有关,在真实设备上它会按预期触发一次。