In iOS 9 our app stopped firing the keyboardwillhide notification. Nothing has changed codewise, was wondering how to get this notification back if possible.
// register for keyboard notifications
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
// register for keyboard notifications
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:nil];
This was being caused by the custom keyboard Pinyin which when that particular keyboard was presented was not firing notification properly. It was device specific to devices with the latest version of that keyboard.
Related
My view controller registers to keyboard notifications (keyboardWillShow, keyboardWillHide).
I launch my app. It is showing the viewcontroller that is registered to keyboard notifications. The keyboard is not visible.
I switch to the sms app and start writing text. While I'm writing, my app gets a notification. The notification is displayed as a banner on the top of the screen.
When I click the banner, my app is opened and immediately gets a keyboard notification.
As far as I can tell, this keyboard notification is related to the keyboard of the SMS.
How do I identify if the keyboard event came from my app or not?
you can remove listening to observers (keyboard notifications) in viewWillDisappear and can start listening to observer again in viewWillAppear, this might solve the problem
- (void)registerForKeyboardNotifications
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWasShown:)
name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillBeHidden:)
name:UIKeyboardWillHideNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(applicationWillEnterBackground:)
name:UIApplicationWillResignActiveNotification
object:nil];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillDisappear:animated];
[self registerForKeyboardNotifications];
}
- (void)deregisterForKeyboardNotifications {
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
[center removeObserver:self name:UIKeyboardWillShowNotification object:nil];
[center removeObserver:self name:UIKeyboardWillHideNotification object:nil];
[center removeObserver:self name:UIApplicationWillResignActiveNotification object:nil];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[self deregisterForKeyboardNotifications];
}
Init section:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification object:nil];
Some method:
- (void) keyboardWillShow:(NSNotification*) aNotification {
// TO DO
}
Dealloc section:
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
So keyboardWillShow is not called after presenting view in iOS 6.1...
In iOS 6.0 this code works perfectly.
In which "init section" are the observers being added? If your view controller is from a storyboard, for example, then it should be in - (id)initWithCoder:(NSCoder *)decoder.
My recommendation, however, is to set up the observers in viewWillAppear and remove them in viewWillDisappear. That way the setup and tear down are "balanced" and only active when the view controller's content is visible.
I got a UITextField, when you click on it there pops up a keyboard. I've added the UITextField using storyboard and now I want to disable a button when the keyboard pops up. How can I do this?
For disabling the button: [self.howButton setEnabled:NO];
But where do I put this?
you can add your keyboards stament to the notifacticon , when it show , then disable the button , when it hide , then enable the button
in the init to add the observer
- (void)init
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillHide) name:UIKeyboardWillHideNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillShow) name:UIKeyboardWillShowNotification object:nil];
}
achieve the show and hide method
- (void)keyboardWillHide
{
[self.howButton setEnabled:YES];
}
- (void)keyboardWillShow
{
[self.howButton setEnabled:NO];
}
finally remove the observer , when the class is dealloc
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
Register for UIKeyboardWillShowNotification
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillShow) name:UIKeyboardWillShowNotification object:nil];
In the keyboardWillShow method disable your button.
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillHide) name:UIKeyboardWillHideNotification object:nil];
You can re-enable the button if required by registering for the above notification.
I am detecting the showing/hiding of the keyboard by adding this code in the ViewDidLoad:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardDidHide:)
name:UIKeyboardDidHideNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
At some point though I want to remove these observers, without calling
[[NSNotificationCenter defaultCenter] removeObserver:self];
because this removes all observers, and I have other observers that I don't want to be removed. How can I remove only those two??
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardDidHideNotification
object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardWillShowNotification
object:nil];
Use the removeObserver:name:object: method of NSNotificationCentre as described in the official documentation, to remove an observer for a particular notification name.
Use [[NsNotificationCenter defaultCenter] removeObserver:self name: UIKeyboardWillShowNotification object:nil]
I have this problem.. I have a view that contains a uitableview with each column is on a different row (is to editing a row, like the address book for example)
everything is ok, but if I click on the last field on the bottom, the keyboard appears, but appear over the field and I can't see nothing....
how can I solve this thing?
thanks in advance
You may need to know whether the keyboard is showing up or not.
Register for a few notifications:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWasShown:)
name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWasHidden:)
name:UIKeyboardDidHideNotification object:nil];
Scroll the row in tableview to a visible position in the listeners: keyboardWillShow keyboardWasShown keyboardWillHide keyboardWasHidden.