问题
How can I remove keyboard notification?
I put observer on keyboard open and close.
I change the view size depend on keyboard is open or close.
回答1:
Try this to remove keyboard open show observers,
NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillHide, object: nil)
you should remove observers either in deinit
Or viewDidDisappear
as per your requirement.
回答2:
You can put the code at two place.
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}
And on this one
deinit{
NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}
回答3:
try below code hope it works for you
override func viewDidDisappear(animated: Bool) {
NSNotificationCenter.defaultCenter().removeObserver(self)
}
来源:https://stackoverflow.com/questions/55395298/remove-keyboard-notification-when-viewcontroller-disappears