Ger Ger - 2 years ago 201
iOS Question

How to add a UIToolbar programmatically to an iOS app?

Can't seem to find a tutorial which does as the question title describes. I'd like to understand just where the UIToolbar needs to be declared and how to get it onto my view layer.

Answer Source

UIToolbar is a subclass of UIView, so the short answer to your question is: just like any other view.

Specifically, this is an example of how to programmatically create a toolbar. The context in this snippet is viewDidLoad of a view controller.

UIToolbar *toolbar = [[UIToolbar alloc] init];
toolbar.frame = CGRectMake(0, 0, self.view.frame.size.width, 44);
NSMutableArray *items = [[NSMutableArray alloc] init];
[items addObject:[[[UIBarButtonItem alloc] initWith....] autorelease]];
[toolbar setItems:items animated:NO];
[items release];
[self.view addSubview:toolbar];
[toolbar release];

See UIToolbar and UIBarButtonItem documentation for details.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download