【发布时间】:2018-08-10 00:25:48
【问题描述】:
当调用键盘时,我将 UIView 向上移动,在模拟器中运行良好,但是当我在实际设备上运行代码时,无论出于何种原因,都会调用 UIKeyboardWillShow 通知两次。我没有使用任何自定义键盘。
在 viewDidLoad 方法中我调用了这个方法。
func registerKeyBoardNotifications(){
NotificationCenter.default.addObserver(self, selector: #selector(keyBoardWillAppear(notification:)), name: .UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillBeHidden(notification:)), name: .UIKeyboardWillHide, object: nil)
}
然后在 viewWillDisappear 中移除这些观察者。
@objc func keyBoardWillAppear(notification: NSNotification){
if let userInfo = notification.userInfo,
let endFrame = userInfo[UIKeyboardFrameEndUserInfoKey] as? NSValue,
let beginFrame = userInfo[UIKeyboardFrameBeginUserInfoKey] as? NSValue,
beginFrame.isEqual(to: endFrame) == false{
let keyboardSize = endFrame.cgRectValue
self.view.frame.origin.y -= keyboardSize.height - keyboardConstant
signUpButton.isEnabled = false
}
}
keyboardWillAppear 处理程序在物理设备上调用了两次,但在模拟器中调用了一次,过去 2 天都在试图解决这个问题。
Xcode 9.4.1 斯威夫特 4.1
【问题讨论】: