What is the simplest way to add UIToolBar to UITableViewController? I'm depending on edit functionality, so I can't change UITableViewController to UIViewController easily.
Asked
Active
Viewed 3.5k times
40
3 Answers
52
No problem at all, UITableViewController
is a subclass of UIViewController
. And it so happens that in iPhone OS 3.0 any UIViewController
(and subclasses) can work in conjunction with a UINavigationController
to provide a context aware toolbar.
In order for this to work you must:
- Make sure that you use a
UINavigationController
to contain all your view controllers that needs a toolbar. - Set the
toolbarsItems
property of the view controller that wants a toolbar.
This is almost as easy as as setting the view controller's title, and should be done the same way. Most probably by overriding the initWithNibName:bundle:
initializer. As an example:
-(id)initWithNibName:(NSString*)name bundle:(NSBundle*)bundle;
{
self = [super initWithNibName:name bundle:bundle];
if (self) {
self.title = @"My Title";
NSArray* toolbarItems = [NSArray arrayWithObjects:
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target:self
action:@selector(addStuff:)],
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch
target:self
action:@selector(searchStuff:)],
nil];
[toolbarItems makeObjectsPerformSelector:@selector(release)];
self.toolbarItems = toolbarItems;
self.navigationController.toolbarHidden = NO;
}
return self;
}
You can also use setToolbarItems:animated:
instead of assigning to the toolbarItems
property, to add and remove toolbar items in an animated fashion on the fly.
-
Is the NavigationController required? I want to add a ToolBar to a TableViewController that is not part of a NavigationController. Do I need to use a NavigationController even though there will only ever be one view in it? – May 25 '11 at 04:53
-
@sirjorj Yes the `UINavigationController` is required to get the *free* toolbar handling. Without it you must manage your own `UIToolbar` view instance. – PeyloW May 25 '11 at 15:44
-
what if i don't want to put buttons in this toolbar, instead, i want to put only a image in the center, what would i do differently? Thanks. – newton_guima Jul 22 '12 at 05:08
41
In order to make PeyloW's recipe to work, I needed to add the following additional line of code:
self.navigationController.toolbarHidden = NO;
Hope that helps...

MyCatsNameIsBernie
- 1,449
- 1
- 12
- 9
-
2Agreed. I had to put that call in the viewDidLoad method, not the initWithNibName override. Then it works great. – Ben Clayton Mar 16 '11 at 14:42
-
For me it worked: `self.navigationController?.isToolbarHidden = false;` – Hussain KMR Behestee Aug 31 '19 at 07:39
14
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
//Initialize the toolbar
toolbar = [[UIToolbar alloc] init];
toolbar.barStyle = UIBarStyleDefault;
//Set the toolbar to fit the width of the app.
[toolbar sizeToFit];
//Caclulate the height of the toolbar
CGFloat toolbarHeight = [toolbar frame].size.height;
//Get the bounds of the parent view
CGRect rootViewBounds = self.parentViewController.view.bounds;
//Get the height of the parent view.
CGFloat rootViewHeight = CGRectGetHeight(rootViewBounds);
//Get the width of the parent view,
CGFloat rootViewWidth = CGRectGetWidth(rootViewBounds);
//Create a rectangle for the toolbar
CGRect rectArea = CGRectMake(0, rootViewHeight - toolbarHeight, rootViewWidth, toolbarHeight);
//Reposition and resize the receiver
[toolbar setFrame:rectArea];
//Create a button
UIBarButtonItem *infoButton = [[UIBarButtonItem alloc]
initWithTitle:@"back" style:UIBarButtonItemStyleBordered target:self action:@selector(info_clicked:)];
[toolbar setItems:[NSArray arrayWithObjects:infoButton,nil]];
//Add the toolbar as a subview to the navigation controller.
[self.navigationController.view addSubview:toolbar];
[[self tableView] reloadData];
}
- (void) info_clicked:(id)sender {
[self.navigationController popViewControllerAnimated:YES];
[toolbar removeFromSuperview];
}
And in Swift 3:
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
//Initialize the toolbar
let toolbar = UIToolbar()
toolbar.barStyle = UIBarStyle.default
//Set the toolbar to fit the width of the app.
toolbar.sizeToFit()
//Caclulate the height of the toolbar
let toolbarHeight = toolbar.frame.size.height
//Get the bounds of the parent view
let rootViewBounds = self.parent?.view.bounds
//Get the height of the parent view.
let rootViewHeight = rootViewBounds?.height
//Get the width of the parent view,
let rootViewWidth = rootViewBounds?.width
//Create a rectangle for the toolbar
let rectArea = CGRect(x: 0, y: rootViewHeight! - toolbarHeight, width: rootViewWidth!, height: toolbarHeight)
//Reposition and resize the receiver
toolbar.frame = rectArea
//Create a button
let infoButton = UIBarButtonItem(title: "Back", style: UIBarButtonItemStyle.plain, target: self, action: #selector(infoClicked))
toolbar.items = [infoButton]
//Add the toolbar as a subview to the navigation controller.
self.navigationController?.view.addSubview(toolbar)
}
func infoClicked() {
//Handle Click Here
}

Ruben Martinez Jr.
- 3,199
- 5
- 42
- 76

user283846
- 199
- 2
- 8
-
This works great for me. I could not add a `UINavigationController`, so manually added a toolbar was the only way to go. Thanks! – codingFriend1 Nov 28 '12 at 11:24
-
1Nice. I think this should be the accepted answer. I wanted to **add a toolbar to uitableviewcontroller**, not enabling uinavigationcontroller. – marko Apr 21 '14 at 11:46
-
This worked well for me but instead of adding `UIBarButtonItems` I added a view with a button from a storyboard to the toolbar. When compiled by Xcode 7.2.1 this worked on devices up to iOS11.4.1 (not tried on iOS12). When I then started to use Xcode 9.2 and Xcode 10, the button did not respond on devices or simulator running iOS11+ (but did on simulator running iOS9 an 10). The 'solution' I came up with was to remove and recreate the toolbar (again) in `viewDidAppear`. Creating the toolbar in `viewDidAppear` and not also in `viewWillAppear` did not work. – ghr Sep 17 '18 at 00:05