0

I have a toolbar and made this code to learn how to put it above the keyboard when I open it however after doing this or the keyboard appears.

#import "ViewController.h"

@interface ViewController ()

@property (weak, nonatomic) IBOutlet UITextView *textField;
@property (weak, nonatomic) IBOutlet UIToolbar *toolbar;


@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.textField.delegate = self;
    self.toolbar.delegate = self;
    self.textField.inputAccessoryView = self.toolbar;

    self.textField.inputView = self.toolbar;
    [self.toolbar removeFromSuperview];
    [self.textField becomeFirstResponder];

}

- (void)textViewDidBeginEditing:(UITextView *)textView
{


    self.textField.inputAccessoryView = self.toolbar;
}

2 Answers2

1

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
Community
  • 1
  • 1
Otávio
  • 735
  • 11
  • 23
  • He not decided yet.I do not know what it I did wrong. Now I added a setFrame but still not working. Thank you for your help! –  May 12 '15 at 22:09
  • 1
    This is the right answer. You've declared your toolbar as `weak` therefore when you `removeFromSuperView` it is released. If you want to keep it around, declare it as `strong` and don't remove from super view. Adding it as a sub view to another view will remove it from the previous view. – squarefrog May 12 '15 at 22:41
0

This line brings up the keyboard

    [self.textField becomeFirstResponder];

When a textfield becomes first responder it automatically brings up the keyboard.

Edit: remove the following line

self.textField.inputView = self.toolbar;

You only need to set the toolbar as inputAccessoryView

Edit2: also remove this line

[self.toolbar removeFromSuperview];
EagerMike
  • 2,032
  • 1
  • 24
  • 40
  • continues without opening the keyboard –  May 12 '15 at 21:32
  • What are you wanting to happen?? – EagerMike May 12 '15 at 21:34
  • I want to open a toolbar above my keyboard. The problem is that after I configured the tollbar neither she nor the keyboard appear. The toolbar in this same storyboard textField that will open the keyboard –  May 12 '15 at 21:38
  • If you tap on the textfield do they both appear? – EagerMike May 12 '15 at 21:40
  • No. None of them appears. You have no idea what can be? I'm with this problem to three hours already. It is the first time I have an application using the tollbar –  May 12 '15 at 21:42
  • Even so. The problem is not this this line. I do not have to use a toolbar that is in a separate place from the storyboard? –  May 12 '15 at 21:59
  • Try not removing the toolbar – EagerMike May 12 '15 at 22:04
  • He not decided yet.I do not know what it I did wrong. Now I added a setFrame but still not working. Thank you for your help! –  May 12 '15 at 22:09