如何隐藏/显示导航栏中的右键

我需要隐藏导航栏中的右键,然后在用户select一些选项后取消隐藏。

不幸的是,以下不起作用:

NO GOOD: self.navigationItem.rightBarButtonItem.hidden = YES; // FOO CODE 

有没有办法?

通过将引用设置为nil来隐藏button,但是如果您想稍后恢复它,则需要挂上它的副本,以便重新分配它。

 UIBarButtonItem *oldButton = self.navigationItem.rightBarButtonItem; [oldButton retain]; self.navigationItem.rightBarButtonItem = nil; //... later self.navigationItem.rightBarButtonItem = oldButton; [oldButton release]; 

就个人而言,在我的应用程序中,我将导航button置于@properties中,以便我可以随意垃圾并重新创build它们,例如:

 //mycontroller.h UIBarButtonItem *rightNavButton; @property (nonatomic, retain) UIBarButtonItem *rightNavButton; //mycontroller.m @synthesize rightNavButton; - (UIBarButtonItem *)rightNavButton { if (!rightNavButton) { rightNavButton = [[UIBarButtonItem alloc] init]; //configure the button here } return rightNavButton; } //later, in your code to show/hide the button: self.navigationItem.rightBarButtonItem = self.rightNavButton; 

设置参考零:

 current_controller_in_navcontroller.navigationItem.rightBarButtonItem = nil; 

另外一定要在navController显示的控制器中调用它,而不是navController本身。

显示:

 [self.navigationItem.rightBarButtonItem.customView setAlpha:1.0]; 

隐藏:

 [self.navigationItem.rightBarButtonItem.customView setAlpha:0.0]; 

你甚至可以animation显示/隐藏

 [UIView animateWithDuration:0.2 animations:^{ [self.navigationItem.rightBarButtonItem.customView setAlpha:1.0]; }]; 

对于Swift 3

 if let button = self.navigationItem.rightBarButtonItem { button.isEnabled = false button.tintColor = UIColor.clear }` 

这是Matt的解决scheme更新为故事板和ARC。 请记住,IBOutlets默认为__weak,因此您需要将其更改为strong,因为它不会太早发布。

 @interface MAGTableViewController () <UITextFieldDelegate> @property (strong, nonatomic) IBOutlet UIBarButtonItem *rightBarButton; @end @implementation MAGTableViewController - (void)viewDidLoad { [super viewDidLoad]; [self.navigationItem setRightBarButtonItem:nil]; } - (IBAction)rightBarButtonItemTapped:(id)sender { [self.view endEditing:YES]; } - (void)textFieldDidBeginEditing:(UITextField *)textField { [self.navigationItem setRightBarButtonItem:self.rightBarButton]; } - (void)textFieldDidEndEditing:(UITextField *)textField { [self.navigationItem setRightBarButtonItem:nil]; } @end 

学分必须去学习这个答案,答案是从这个问题:

在iOS-7中隐藏并显示左侧导航栏button

这是一个更简单的答案。

 //hide and reveal bar buttons -(void) hideAndDisableLeftNavigationItem { [self.navigationItem.leftBarButtonItem setTintColor:[UIColor clearColor]]; [self.navigationItem.leftBarButtonItem setEnabled:NO]; } -(void) showAndEnableLeftNavigationItem { [self.navigationItem.leftBarButtonItem setTintColor:[UIColor blueColor]]; [self.navigationItem.leftBarButtonItem setEnabled:YES]; } 

然后你只需要像(IBAction)那样引用你所需要的方法:

 [self hideAndDisableLeftNavigationItem];//[self showAndEnableLeftNavigationItem]; to show again 

我尝试了所有其他方法,没有工作,甚至引用我的button作为@property (...) UIBarButtonItem....没有任何工作,直到我发现这一点。

SWIFT 2.2

在swift 2.2中,self.navigationItem不起作用。 而是创build一个NavigationItem的出口(我把它命名为“nav”下面)并使用它。

另外下面的build议不适用于我使用Xcode 7.3和Swift 2.2

  nav.leftBarButtonItem?.customView?.hidden = true 

所以我用@Matt J的想法如下(我有2项在左边):

  1. 为导航栏中的项目创build出口和variables以存储它们

     @IBOutlet weak var settingItem: UIBarButtonItem! @IBOutlet weak var logoItem: UIBarButtonItem! var sav_settingItem: UIBarButtonItem = UIBarButtonItem() var sav_logoItem: UIBarButtonItem = UIBarButtonItem() 
  2. 将项目保存在viewDidLoad()

     sav_settingItem = nav.leftBarButtonItems![0] sav_logoItem = nav.leftBarButtonItems![1] 
  3. 隐藏设置为零

     nav.leftBarButtonItem = nil nav.leftBarButtonItem = nil 
  4. 显示他们

     if (nav.leftBarButtonItem == nil) { nav.leftBarButtonItem = sav_settingItem nav.leftBarButtonItems?.append(sav_logoItem) return } 

显示:

 [self.navigationItem.rightBarButtonItem.customView setHidden:NO]; 

隐藏:

 [self.navigationItem.rightBarButtonItem.customView setHidden:YES]; 

我的解决scheme

 self.navigationItem.rightBarButtonItem.customView.hidden=NO; 

Swift 2

招!

隐藏:

 if let btn = self.tabBarController!.navigationItem.rightBarButtonItem { btn.enabled = false btn.title = "" } 

显示:

 if let btn = self.tabBarController!.navigationItem.rightBarButtonItem { btn.enabled = true btn.title = "ButtonName" } 

显示:

 //set navigationItem tint color white self.navigationItem.rightBarButtonItem.tintColor = [UIColor whiteColor]; 

隐藏:

 //set navigationItem tint clear white self.navigationItem.rightBarButtonItem.tintColor = [UIColor clearColor]; 
  1. 假设你可以引用特定的栏button作为variablesxxxButton

(请打开助理编辑器,控制+拖动xxxbutton到YourViewController类作为出口“xxxButton”)。

或者你可以使用像let xxxButton = navigationBar.buttons[1]

  1. Hide xxxButton.customView = UIView() or navigationItem.rightBarButtonItems?.remove(at: (navigationItem.rightBarButtonItems?.index(of:xxxButton)!)!)

  2. 显示xxxButton.customView = nilnavigationItem.rightBarButtonItems?.insert(newElement: xxxButton, at:SOME_INDEX)

希望有帮助。

隐藏:

 if let topItem = self.navigationController?.navigationBar.topItem { topItem.rightBarButtonItem = nil } 
Interesting Posts