I have a tab bar controller with 4 views controller, and have this tab bar controller in a navigation controller.
I want to display a UIBarButtonItem for just one specific view controller of the tab bar controller.
I tried to use the following
if (tabBarController.selectedViewController == customTourViewController)
{
[tabBarController.navigationItem setRightBarButtonItem:done];
}
But the button doesn't show up.
If I put every view controller in a navigation controller, then the button shows up for only that view, but I end up having 2 navigation bars.
Is there any way I can implement the first solution? Thanks.
To add a tab, first drag a new View Controller object to the storybard. Next control-drag from the tab bar controller to new view controller and select view controllers under Relationship Segue . Your tab bar controller will update with a new tab.
A tab bar controller is a powerful UI component for iOS apps. It's a container view, and you use it to group view controllers together. They give your app's user access to the most important screens of your app.
Way 1: Touch “Settings” -> “Display” -> “Navigation bar” -> “Buttons” -> “Button layout”. Choose the pattern in “Hide navigation bar” -> When the app opens, the navigation bar will be automatically hidden and you can swipe up from the bottom corner of the screen to show it.
In my individual view controllers for the individual tabs, I have the following in the one that needs the button:
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:@"Done"
style:UIBarButtonSystemItemDone target:nil action:nil];
self.tabBarController.navigationItem.rightBarButtonItem = rightButton;
}
And in the view controllers that don't need the button, I have:
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
self.tabBarController.navigationItem.rightBarButtonItem = nil;
}
So, if it's not working for you, I'm not sure if it's your reference to tabBarController
without the self
designation (if I omit the self
I get a compiler error). And where is this code because if it's in your tabBarController subclass, then you want self.navigationItem.rightBarButtonItem
, right? Do you have your own ivar defined for that variable name? Or are you sure that done
is defined properly (i.e. not nil
)? Or are you sure this code is being called at all (perhaps set a breakpoint or insert a NSLog
and make sure this code is being reached)?
Alternatively you can implement viewWillDisappear in the same view where you need the button.
-(void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
self.tabBarController.navigationItem.rightBarButtonItem = nil;
}
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