Swift - KeyboardWillHide和KeyboardDidShow通知几乎同时触发

时间:2016-03-27 23:15:57

标签: ios swift keyboard nsnotifications

我尝试使用键盘通知,因此我可以根据键盘是显示还是隐藏来向上或向下移动滚动视图中的信息。我的问题是当我第一次点击任何文本字段时。我无法理解为什么当KeyboardDidShow的通知触发时它会触发KeyboardWillHide的通知......

这是相关的代码。

func registerKeyboardNotifications() {
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardDidShow:", name: UIKeyboardWillShowNotification, object: nil)
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillHide:", name: UIKeyboardWillHideNotification, object: nil)
}

func unregisterKeyboardNotifications() {
    NSNotificationCenter.defaultCenter().removeObserver(self)
}

func keyboardDidShow(notification: NSNotification) {
    NSLog("Keyboard did show")
}

func keyboardWillHide(notification: NSNotification) {
    NSLog("Keyboard will hide")
}

这是输出:

0 个答案:

没有答案