I have a nav controller with a toolbar. I made the toolbar also appear on top of the keyboard when the keyboard appears. When I dismiss the keyboard, the toolbar disappears, leaving a black rectangle at the bottom of the screen, just where the toolbar should be without the keyboard.
Here’s how I init the toolbar:
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self.navigationController setToolbarHidden:NO];
// this makes sure the toolbar appears on top of the keyboard
// instead of going below it.
// _nameText is a UITextField
_nameText.inputAccessoryView = self.navigationController.toolbar;
}
This is how I hide the keyboard:
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
[self.navigationController setToolbarHidden:NO]; // this doesn't help
return NO;
}
I tried also doing [self.view setNeedsLayout], but that didn’t work.
EDIT: I suspect this may have to do with the fact that I assign the toolbar to be the input accessory view of my text field. I think that the text field hides its accessory view when the keyboard goes away. I still don’t know how to override that behavior though.
EDIT 2: I discovered that self.navigationController.toolbar.superview is nil after the keyboard is gone.
OK, so while I couldn’t solve the problem head-on, I found an acceptable workaround.
Create a .xib for your toolbar
Load the toolbar from (1) into an object
assign that object to the
inputAccessoryViewproperty of your text fieldset up the target and the actions for the buttons in this toolbar, so you can respond to clicks
You are now all set. Your original toolbar (which you presumably have created in the Interface Builder) is only visible when the keyboard is hidden. When the keyboard is visible, the original toolbar cannot be seen, but your other one (created with the steps described above) now appears above the keyboard. Bingo!
If anyone has a more elegant solution to this problem, I’d be happy to hear about it 🙂