In my quest for my first iPhone app I have posted about the correct way to handle the return key on the iOS keyboard. Now I need to figure out the toolbar above the keyboar
Not sure if it's too late. I put my solution about adding a UIToolbar into UIKeyboard, and The keyboard types changes with the UITextField content, and handling the UIBarButtonItem with "Previous", "Next" and "Done" at Github. And if the TextFields at different sections of the tableview, it turns to be more tricky.
I think your buttons are just not visible because of the following code:
self.keyboardToolbar.barStyle = UIBarStyleBlackTranslucent;
self.keyboardToolbar.tintColor = [UIColor darkGrayColor];
Try using
self.keyboardToolbar.barStyle = UIBarStyleBlack;
self.keyboardToolbar.tintColor = [UIColor whiteColor];
instead
UIBarButtonItem
isn't a UIView
subclass, so you can't add it as a subview of the UIToolbar
. Instead, use the – setItems:animated:
method on your toolbar to add those buttons.
The problem must be in the place from where you add this sub-accessory view.
I have managed to use your code in my current project with the addition of adding the toolbar on top of a UIDatePicker that gets initiated by a textfield inside a UITableView. I'm using static cells.
It works for me.
I have the UITextFields as IBOutlets and only use this code to set the InputAccessoryView
[textFieldName setInputAccessoryView:self.keyboardToolbar];
[textFieldEmail setInputAccessoryView:self.keyboardToolbar];
[textFieldPhone setInputAccessoryView:self.keyboardToolbar];
[textFieldDate setInputAccessoryView:self.keyboardToolbar];
[textFieldSize setInputAccessoryView:self.keyboardToolbar];
Well, the next and prev buttons work like a charm (but only on the way down the table). I still had to manually scroll the table to the top when going back up. For some reason the UITableViewController only handles the scrolling down scenario, which I found quite odd, but here is my code snippet for previous.
- (void) gotoPrev {
if (self.activeField == textFieldName) { // name --> nowhere
return;
}
else if (self.activeField == textFieldEmail) { // email --> name
[textFieldName becomeFirstResponder];
}
else if (self.activeField == textFieldPhone) { // phone --> email
[textFieldEmail becomeFirstResponder];
// Scroll jeegar
NSIndexPath * myIndexPath= [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView scrollToRowAtIndexPath:myIndexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
}
else if (self.activeField == textFieldDate) { // date --> phone
[textFieldPhone becomeFirstResponder];
}
else if (self.activeField == textFieldSize) { // people --> date
[textFieldDate becomeFirstResponder];
}
}