UIToolbar on each page of UINavigationController
Asked Answered
A

3

11

I have an application which runs on a UINavigationController. Now I would like to add a UIToolbar element to the bottom of each screen. The Toolbar on the bottom should the be customizable for the ViewController that is currently being displayed. My first idea was to simply add the toolbar to the navigationController view and tag it, in the viewController I thought I would then be able to retrieve the UIToolbar element. I have the following code:

In my AppDelegate:

// Get instance of Toolbar  (navController is an instance of UINavigationController and TOOLBAR_TAG a constant)
UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 440, 320, 40)];
toolbar.tag = TOOLBAR_TAG;
[navController.view addSubview:toolbar];

In my viewController I tried this:

UIToolbar *toolbar = [self.navigationController.view viewWithTag:TOOLBAR_TAG];
toolbar.barStyle = UIBarStyleBlack;

Yet this gives me an error saying that toolbar in my case is a "UILayoutContainerView" object, not an UIToolbar object. Hence this idea seems to be a dead end.

How did others solve this issue?

Androsphinx answered 22/2, 2010 at 16:47 Comment(0)
A
42

UINavigationController already has a toolbar. Just use

[self.navigationController setToolbarHidden:NO];

in the topmost view controller and

[self setToolbarItems:items];

in all your view controllers, where items is an NSArray of that view controller's toolbar items.

EDIT: As for why your solution isn't working: your TOOLBAR_TAG is probably not unique, that's why you're getting another subview. But as I said, you should use the included toolbar anyway.

Absalom answered 22/2, 2010 at 16:49 Comment(4)
Wow that is a service I did not expect, I even searched the documentation yet did not find this. Very cool, thanks!Androsphinx
you're welcome. =) it's in the UINavigationController documentation, btw: developer.apple.com/iphone/library/documentation/UIKit/…Building
You can override your UINavigationController pushController method to avoid copy/paste and set the toolbar items here.Vain
how to make it work for iPad? I am using SplitView and navigation controller. the toolbar never shows up on iPad, but does on iPhone. Is there something wrong I'm doing?Linguistic
I
1

To easily display the UINavigationController bottom toolbar, you can click on the "Show Toolbar" checkbox which is reachable from the inspector with "Navigation Controller" object selected. I hope this may help :)

Imelda answered 24/7, 2011 at 17:57 Comment(0)
A
0

From a design perspective I would not recommend this UNLESS you know the toolbar will be present for each view in the stack. The second you start hiding/showing the toolbar for different views in the stack you will see that that animation (toolbar slides out/in with views) doesn't animate like you expect.

If you need toolbars for specific views put them in those views, since toolbar are contextual to the view, not to the nav stack as a whole.

Agronomics answered 13/5, 2015 at 14:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.