我被困在问题“keyboardWillShow”触发两次,但“keyboardWillHide”调用一次。keyboardWillShow fire两次
这里是an example,我在“keyboardWillShow”发射后立即打印键盘尺寸。 我也把断点放在“viewDidLoad”中,观察者只注册一次。 我已经添加了两个元素“UITextField”和“UITextView”,两者都是相同的行为。
我使用的是iOS 9.2,迅速郎,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")
}
}
UPDATE
第一次与大小触发一次。 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)
我记得我之前得到过这个,它还有一个Xcode或者iOS 9的bug ......所以我添加一个BOOL然后在'keyboardWillShow'上设置为YES,然后执行代码 – Tj3n
听起来不像您在此处使用的是除默认键盘之外的任何内容,但在使用第三方键盘的情况下,系统将使用系统键盘的高度调用一次,然后再次使用您使用的实际键盘的高度一旦扩展加载。 – pbush25