I’m adding UIToolBar to my UITextField using:
- (void)viewDidLoad {
[super viewDidLoad];
self.email.delegate = self;
self.password.delegate = self;
UIToolbar* toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 44)];
UIBarButtonItem* previous = [[UIBarButtonItem alloc] initWithTitle:@"Anterior" style:UIBarButtonItemStyleBordered target:self action:@selector(move:)];
UIBarButtonItem* next = [[UIBarButtonItem alloc] initWithTitle:@"Próximo" style:UIBarButtonItemStyleBordered target:self action:@selector(move:)];
UIBarButtonItem* space = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:(UIBarButtonSystemItemFlexibleSpace) target:nil action:nil];
UIBarButtonItem* ok = [[UIBarButtonItem alloc] initWithTitle:@"Ok" style:UIBarButtonItemStyleBordered target:self action:@selector(ok:)];
[toolbar setItems:[[NSArray alloc] initWithObjects:previous, next, space, ok, nil]];
[toolbar setTranslucent:YES];
[toolbar setTintColor:[UIColor blackColor]];
for (UIView* view in self.view.subviews) {
if ([view isKindOfClass:[UITextField class]]) {
[(UITextField*)view setInputAccessoryView:toolbar];
}
}
}
Now I’m adding a UIScrollView to my UIViewController and my UIToolBar doesn’t show anymore. What am I missing? I think I’m adding the toolbar to the view and not to the scrollview, and because the scrollview is above the view, I can’t see the toolbar. How can I fix it? I need to add to scroll view to move the content behind the keyboard.
You say that you have added a UIScrollView to your UIViewController. I assume that means that the
self.emailandself.passwordare now subviews of this scroll view and not subviews of your main UIViewController’sview. So the code that iterates over the subviews of theviewproperty will not find your UITextFields.So your code:
should look something like (I assume that the scrollView is a property):
An alternative method to dealing with this is to use your UITextFieldDelegate methods to add the
inputAccessoryViewproperty in response to beginning editing. So for example,