Your line [self.toolbar removeFromSuperview];
is causing the toolbar to become nil
, so it is not appearing.
And as @jammycoder said you only need to set inputAccessoryView
of the text field. So do as he suggested and remove the line self.textField.inputView = self.toolbar;
Edit
I have setup a simple project and put a UIToolbar
and a UITextView
on the view controller, in the storyboard. Those two views are connected (IBOutlets) to the ViewController as you probably did,.
If I use your code it indeed does not show the keyboard, why? Because you are assigning the inputView
of the UITextView
.
So let's debug your code. First of all add bunch of NSLog
statements like this.
- (void)viewDidLoad
{
[super viewDidLoad];
self.textField.delegate = self;
NSLog(@"1 %@", NSStringFromCGRect(self.toolbar.frame));
self.textField.inputAccessoryView = self.toolbar;
NSLog(@"2 %@", NSStringFromCGRect(self.toolbar.frame));
self.textField.inputView = self.toolbar;
NSLog(@"3 %@", NSStringFromCGRect(self.toolbar.frame));
[self.toolbar removeFromSuperview];
NSLog(@"4 %@", NSStringFromCGRect(self.toolbar.frame));
[self.textField becomeFirstResponder];
}
- (void)textViewDidBeginEditing:(UITextView *)textView
{
NSLog(@"5 %@", NSStringFromCGRect(self.toolbar.frame));
self.textField.inputAccessoryView = self.toolbar;
NSLog(@"6 %@", NSStringFromCGRect(self.toolbar.frame));
}
This will print something like:
2015-05-14 00:13:46.313 ToolbarKeyboard[8367:4583306] 1 {{0, 524}, {320, 44}}
2015-05-14 00:13:46.314 ToolbarKeyboard[8367:4583306] 2 {{0, 524}, {320, 44}}
2015-05-14 00:13:46.314 ToolbarKeyboard[8367:4583306] 3 {{0, 524}, {320, 44}}
2015-05-14 00:13:46.315 ToolbarKeyboard[8367:4583306] 4 {{0, 524}, {320, 44}}
2015-05-14 00:13:46.491 ToolbarKeyboard[8367:4583306] 5 {{0, 0}, {320, 0}}
2015-05-14 00:13:46.491 ToolbarKeyboard[8367:4583306] 6 {{0, 0}, {320, 0}}
So, we discovered that, somehow, your bar height became 0. And, to be honest I don't know why, but lets continue our debugging.
As pointed by @jammycoder you don't need to the the inputView
of the UITextView
you only need to do that if you want a custom keyboard. So lets change our code to be like this:
- (void)viewDidLoad
{
[super viewDidLoad];
self.textField.delegate = self;
NSLog(@"1 %@", NSStringFromCGRect(self.toolbar.frame));
self.textField.inputAccessoryView = self.toolbar;
NSLog(@"2 %@", NSStringFromCGRect(self.toolbar.frame));
[self.toolbar removeFromSuperview];
NSLog(@"3 %@", NSStringFromCGRect(self.toolbar.frame));
[self.textField becomeFirstResponder];
}
- (void)textViewDidBeginEditing:(UITextView *)textView
{
NSLog(@"4 %@", NSStringFromCGRect(self.toolbar.frame));
self.textField.inputAccessoryView = self.toolbar;
NSLog(@"5 %@", NSStringFromCGRect(self.toolbar.frame));
}
This will print that the height of the toolbar remained unchanged. And the toolbar is being shown above the keyboard.
Until here I was using a real device to debug, decided to use a simulator. Wait, it is not opening the keyboard there! It seems to be a new feature of Xcode 6, so I did a quick search and found this stackoverflow: Xcode 6: Keyboard does not show up in simulator Did what was saying in the most voted answer and the everything is working just fine.
So, in short. The following minimum code will produce the desired behavior. And if you are using the simulator to test/debug it make sure to do what is described here: https://stackoverflow.com/a/24497773/3927536
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UITextView *textField;
@property (weak, nonatomic) IBOutlet UIToolbar *toolbar;
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.textField.inputAccessoryView = self.toolbar;
[self.textField becomeFirstResponder];
}
@end