在我的应用程序中,我希望能够收到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)
}
这里,其他使用相同通知的视图控制器可以打印出"keyboard will show 2",但是这个控制器没有打印。iOS 12中是否有任何新变化导致了这种情况?否则,为什么它没有被调用?
deinit
中记录日志。我敢打赌它过早地消失了。 - matt