I've programmatically created some UINavigationControllers and added them to a UITabBarController. Everything seems to work fine but I wanted to add a cancel button to the navigation controller but it never shows up. I've tried multiple ways but I can't seem to affect the display of the navigation items at all and I've followed multiple examples from here and other sites but nothing happens.
MyTableViewController *mtvc = [[MyTableViewController alloc] init];
UINavigationController *myNavController = [[[UINavigationController alloc] initWithRootViewController:mtvc] autorelease];
myNavController.navigationBar.barStyle = UIBarStyleBlackOpaque; // this works
[mtvc release];
// TODO: figure out why added buttons aren't showing
UIBarButtonItem *closeButton = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(shutDown)] autorelease];
myNavController.navigationItem.leftBarButtonItem = closeButton; // never shows up
I also tried adding the button this way
[myNavController.navigationItem setLeftBarButtonItem:closeButton animated:NO]; // also doesn't do anything
I started getting frustrated so I also tried some other things just to see if I could affect anything, but to no avail
myNavController.title = @"test"; // does nothing
I've tried doing it before and after the navigations controllers were added to the UITabBarController and that didn't help. I've also tried rightBarButtonItem and tried using initWithTitle: instead of initWithBarButtonSystemItem.
Would someone please illuminate me? Clearly, I'm doing this the wrong way.
@MainActor class UINavigationController : UIViewController A navigation controller is a container view controller that manages one or more child view controllers in a navigation interface. In this type of interface, only one child view controller is visible at a time.
The initial bar button is defined in the storyboard, by dragging a UIBarButtonItem out of the object library and into the navigation bar. The sample also shows how to create and add each button type using code.
Here’s how to set the navigation item’s prompt: Apply a custom background to a UINavigationBar by adding a bar tint color or background image. The sample sets the background image of a navigation bar like this:
Each time the top-level view controller changes, the navigation controller updates the navigation bar accordingly. Specifically, the navigation controller updates the bar button items displayed in each of the three navigation bar positions: left, middle, and right. Bar button items are instances of the UIBarButtonItem class.
Try adding the bar buttons in the loadView
method of MyTableViewController
like the follwing.
UIBarButtonItem *closeButton = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(shutDown)] autorelease];
self.navigationItem.leftBarButtonItem = closeButton;
I guess that should work.
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