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?
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.
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 :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With