UIKeyboardWillShowNotification

I have an observer for UIKeyboardWillShowNotification and UIKeyboardWillHideNotification .

 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil]; 

Everything works, except that it works when the viewController is not currently displayed.

I tried comparing with self.navigationcontroller.topViewController , but this does not work when I have a modal view presented as a topViewController that is under the modal view controller.

+4
source share
2 answers

If you use the UIViewController , you can register your instance for keyboard notifications when the view becomes visible inside viewWillAppear: and then deregister when the view is hidden inside viewWillDisappear: This way, you won’t receive notifications when the view is not visible.

Hope this helps!

+7
source

If you want to respond to this notification when the viewController is visible, then just check if it is visible at the beginning of the function:

 - (void)keyboardWillShow:(NSNotification *)notification { if ([self.view window]) //means is visible //do something else //return } 
+3
source

Source: https://habr.com/ru/post/1484863/


All Articles