Adding UIToolbar On Top of Keyboard in Objective-C | iOS Programmer Guide

We can add UIToolbar on top of the keyboard via code in iOS. First, create and instantiate an UIToolbar as given below:

UIToolbar *keyboardToolbar = [[UIToolbar alloc] initWithFrame: CGRectMake(0,0, 320, 44)];

Then create the required buttons to add them to the created UIToolbar. here, it is keyboardToolbar created:

UIBarButtonItem *flex = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil] autorelease];

UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemReply target:self action:@selector(cancelKeyboard)];


Then add these created bar buttons to UIToolbar as below:

[keyboardToolbar setItems:[NSArray arrayWithObjects:flex, cancelButton, nil] animated:NO]; subjectTextField.inputAccessoryView = keyboardToolbar;

Now the UIToolbar is ready. Finally, let us assign this UIToolbar to the UITextField as given in the below objective-c code snippet: (Note: Consider the textField as the available UITextField)

textField.inputAccessoryView = keyboardToolbar;