在我的应用中,我希望收到UIResponder.keyboardWillShowNotification
的通知,以更新文本字段的y位置。它在iOS 12之前运行;现在,它在我的一个视图控制器中没有被调用(它适用于其他视图控制器)。这是我执行此操作的代码:
@objc func keyboardWillShow(_ notification: Notification) {
print("keyboard will show 2")
guard let frameValue: NSValue = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue else {
return
}
let keyboardFrame = frameValue.cgRectValue
UIView.animate(withDuration: animationTime) {
self.addViewBottomConstraint.constant = keyboardFrame.size.height
self.view.layoutIfNeeded()
print("Bottom contraint height = \(self.addViewBottomConstraint.constant)")
}
}
@objc func keyboardWillHide(_ notification: Notification) {
UIView.animate(withDuration: animationTime) {
self.addViewBottomConstraint.constant = 0
self.view.layoutIfNeeded()
}
}
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(_:)), name: UIResponder.keyboardWillShowNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(_:)), name: UIResponder.keyboardWillHideNotification, object: nil)
}
此处,“键盘将显示2”未打印,但为其他具有相同通知的视图控制器打印。 iOS 12中有什么新原因导致此问题吗?否则,是否有不被调用的特定原因?谢谢你的帮助。