Hide the rightBarButtonItem of a navigation controller

Deadly 提交于 2019-12-05 13:11:56

问题


Does anyone know how to hide a rightBarButtonItem of a UINavigationController? In my application, I have an edit button as a rightBarButtonItem of a UINavigationController. I want to hide this ? UIBarButton` when some operations are done.


回答1:


To Hide the right button: self.navigationItem.rightBarButtonItem = nil;

Now, to show it:

  1. If you setup the right button in your view controller by assigning it to self.editButtonItem then simply assign it again in order to show it:

    self.navigationItem.rightBarButtonItem = self.editButtonItem;

  2. If you setup the right button in your view controller by allocating and initing a UIBarButtonItem, then simply keep a reference to the UIBarButtonItem in your view controller, and assign it again when you need to show it.




回答2:


If you need to hide/show the button based on some condition, try this:

if (condition) { 
    self.navigationItem.rightBarButtonItem.title = @"";
    self.navigationItem.rightBarButtonItem.enabled = NO;
} else {
    self.navigationItem.rightBarButtonItem.title = @"my button title";
    self.navigationItem.rightBarButtonItem.enabled = YES;
}

This way you don't have to save a reference to the button in a property or worry about wiring up the action on a new button.




回答3:


Try

self.navigationItem.rightBarButtonItem = nil;

When you want it back though you will have to instanciate a button i.e.

UIBarButtonItem *rightBarButton = 
 [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch
                                               target:self
                                               action:@selector(searchBar:)];
self.navigationItem.rightBarButtonItem = rightBarButton;
[rightBarButton release];


来源:https://stackoverflow.com/questions/3042818/hide-the-rightbarbuttonitem-of-a-navigation-controller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!