I need to hide the right button in the Navigation Bar, then unhide it after the user selects some options.
Unfortunately, the following doesn\'t work:
Show:
[self.navigationItem.rightBarButtonItem.customView setHidden:NO];
Hide:
[self.navigationItem.rightBarButtonItem.customView setHidden:YES];
Show:
[self.navigationItem.rightBarButtonItem.customView setAlpha:1.0];
Hide:
[self.navigationItem.rightBarButtonItem.customView setAlpha:0.0];
You can even animate its showing/hiding
[UIView animateWithDuration:0.2 animations:^{
[self.navigationItem.rightBarButtonItem.customView setAlpha:1.0];
}];
Swift 2:
Trick!
Hide:
if let btn = self.tabBarController!.navigationItem.rightBarButtonItem {
btn.enabled = false
btn.title = ""
}
Show:
if let btn = self.tabBarController!.navigationItem.rightBarButtonItem {
btn.enabled = true
btn.title = "ButtonName"
}
For swift 5 to hide rightBarButtonItem
self.navigationItem.rightBarButtonItem?.customView?.isHidden = true
Show:
//set navigationItem tint color white
self.navigationItem.rightBarButtonItem.tintColor = [UIColor whiteColor];
Hide:
//set navigationItem tint clear white
self.navigationItem.rightBarButtonItem.tintColor = [UIColor clearColor];
xxxButton
(please open Assistant Editor, Control+Drag xxx button to YourViewController class as outlet "xxxButton").
or you can use something like let xxxButton = navigationBar.buttons[1]
Hide
xxxButton.customView = UIView()
or
navigationItem.rightBarButtonItems?.remove(at: (navigationItem.rightBarButtonItems?.index(of:xxxButton)!)!)
Show
xxxButton.customView = nil
or
navigationItem.rightBarButtonItems?.insert(newElement: xxxButton, at:SOME_INDEX)
Hope helpful.