Click here to Skip to main content
12,816,654 members (35,009 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


1 bookmarked
Posted 3 Apr 2013

Simple UIToolbar as Keyboard Accessory

, 3 Apr 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
UITextField has a property called inputAccessoryView that can be set to a UIToolbar to give the user something to do after inputting text.

UITextField has a property called inputAccessoryView that can be set to a UIToolbar to give the user something to do after inputting text.

I have already set up text field as a synthesized property of my view controller, called nameTextField, and linked it to by nib.

viewDidLoad is a good place to create this toolbar, and I’ll call it keyboardToolBar.

UIToolbar * keyboardToolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
keyboardToolBar.barStyle = UIBarStyleBlackTranslucent;

This gives us a black, translucent UIToolbar that is 320 x 50. We can set the items by passing an array of UIBarButtonItem to [keyboardToolBar setItems:(NSArray*)]. The constructor of a UIBarButtonItem that I am using [[UIBarButtonItem alloc] initWithTitle:(NSString *)title style:(UIBarButtonItemStyle)style target:(id)target action:(SEL)action]; 

UIBarButtonItemStyle are predefined styles you can use, I suggest you browse through the options. The target is the object to send action (a method/message) to, which is triggered when the button is pressed. You need to implement these methods to avoid a crash.

I have decided on three buttons, and a spacer in between. The spacer is simply a UIBarButtonItem initialized with [UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemFlexibleSpace target:nil action:nil];. This pushes all previous buttons to be left aligned and all buttons after it to be right aligned.

All together, we have:

[keyboardToolBar setItems: 
[NSArray arrayWithObjects:[[UIBarButtonItem alloc]initWithTitle:@"Cancel"
style:UIBarButtonItemStyleBordered target:self
[[UIBarButtonItem alloc]initWithBarButtonSystemItem:
target:nil action:nil],
[[UIBarButtonItem alloc] initWithTitle:@"Next"
style:UIBarButtonItemStyleBordered target:self
[[UIBarButtonItem alloc]initWithTitle:@"Done"
style:UIBarButtonItemStyleDone target:self
action:@selector(doneWithTextField)], nil]];

Finally, we make the connection from our text field to the new toolbar with:

nameTextField.inputAccessoryView = keyboardToolBar;

You can grab the Xcode project here.

The post Simple UIToolbar as Keyboard Accessory appeared first on Code and Prose.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170308.1 | Last Updated 3 Apr 2013
Article Copyright 2013 by ClayJones
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid