Reputation: 6691
I have made a custom navigation bar, same for all the views by implementing the method below -:
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
// viewController.navigationItem.rightBarButtonItem = cancelButton;
// -- Adding INFO button to Navigation bar --
UIBarButtonItem *infoButton = [[UIBarButtonItem alloc]
initWithTitle:@"i"
style:UIBarButtonItemStyleBordered
target:self
action:@selector(showInfo)];
infoButton.tag = 10;
self.navCntrl.topViewController.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:infoButton, nil];
self.navCntrl.navigationBar.tintColor = [UIColor colorWithRed:45/255.0 green:77/255.0 blue:67/255.0 alpha:1];
// NSLog(@"Inside implemented method");
}
of UINavigationControllerDelegate
.
In the above method I have added a right button to the navigation Item. Now I want to hide this right button in a particular view. How can I achieve this ? Thanks.
Upvotes: 1
Views: 9888
Reputation: 21
For those that are still looking for an answer, this code worked for me in AppDelegate.m
- (BOOL)application:(UIApplication *)applicationdidFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
...
// Get rid of the edit button in UITabBarController's moreNavigationController
tabBarController.customizableViewControllers = nil;
...
}
Upvotes: 0
Reputation: 69499
Just check if the viewController pushed of tf the type where you do not want the righ bar button:
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
// Replace the YourViewController with the type of the viewcontroller
// you want not the have the right bar button.
if ([viewController isKindOfClass:[YourViewController class]]) {
return;
}
// viewController.navigationItem.rightBarButtonItem = cancelButton;
// -- Adding INFO button to Navigation bar --
UIBarButtonItem *infoButton = [[UIBarButtonItem alloc]
initWithTitle:@"i"
style:UIBarButtonItemStyleBordered
target:self
action:@selector(showInfo)];
infoButton.tag = 10;
self.navCntrl.topViewController.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:infoButton, nil];
self.navCntrl.navigationBar.tintColor = [UIColor colorWithRed:45/255.0 green:77/255.0 blue:67/255.0 alpha:1];
// NSLog(@"Inside implemented method");
}
Upvotes: 0
Reputation: 17535
Try to use this one
self.navigationItem.rightBarButtonItem = nil;
self.navigationItem.rightBarButtonItem.enabled = NO;
Upvotes: 7
Reputation: 25011
A nice way to do it would be to have your view controllers implement a protocol. You pick the name, but it can be something like CustomNavigationCustomization
, and have a single method:
@protocol CustomNavigationCustomization
- (BOOL)shouldShowRightButton;
@end
Then, you could change your method to something like this:
- (void)navigationController:(UINavigationController *)navigationController
willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
BOOL shouldShowRightButton = YES;
if ([viewController conformsToProtocol:@protocol(CustomNavigationCustomization)) {
UIViewController <CustomNavigationCustomization> *customizableViewController =
(UIViewController <CustomNavigationCustomization>)viewController;
shouldShowRightButton = [customizableViewController shouldShowRightButton];
}
if (shouldShowRightButton) {
// viewController.navigationItem.rightBarButtonItem = cancelButton;
// -- Adding INFO button to Navigation bar --
UIBarButtonItem *infoButton = [[UIBarButtonItem alloc] initWithTitle:@"i"
style:UIBarButtonItemStyleBordered
target:self
action:@selector(showInfo)];
infoButton.tag = 10;
self.navCntrl.topViewController.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:infoButton, nil];
self.navCntrl.navigationBar.tintColor = [UIColor colorWithRed:45/255.0 green:77/255.0 blue:67/255.0 alpha:1];
// NSLog(@"Inside implemented method");
}
}
Note that the method in the navigation controller delegate is very defensive: it checks if your view controller conforms to the protocol, and only then it invokes the method. This way, you don't need to conform to the protocol in most of your view controllers, only in those you wish to customise.
Upvotes: 0
Reputation: 61
In viewDidLoad, try
self.navigationItem.rightBarButtonItem = nil;
And in viewWillDisappear, don't forget to put it back.
Upvotes: 6