我需要隐藏导航栏中的右侧按钮,然后在用户选择某些选项后再显示它。
不幸的是,下面的代码无效:
NO GOOD: self.navigationItem.rightBarButtonItem.hidden = YES; // FOO CODE
有没有办法?
我需要隐藏导航栏中的右侧按钮,然后在用户选择某些选项后再显示它。
不幸的是,下面的代码无效:
NO GOOD: self.navigationItem.rightBarButtonItem.hidden = YES; // FOO CODE
有没有办法?
将引用设置为nil来隐藏按钮,但是如果以后想要恢复它,您需要保留它的副本以便重新分配。
UIBarButtonItem *oldButton = self.navigationItem.rightBarButtonItem;
[oldButton retain];
self.navigationItem.rightBarButtonItem = nil;
//... later
self.navigationItem.rightBarButtonItem = oldButton;
[oldButton release];
个人而言,我会将我的导航按钮制作成 @properties,在需要时可以随意删除并重新创建,示例如下:
//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;
对于 Swift 3
if let button = self.navigationItem.rightBarButtonItem {
button.isEnabled = false
button.tintColor = UIColor.clear
}`
将引用设置为nil:
current_controller_in_navcontroller.navigationItem.rightBarButtonItem = nil;
请确保在当前由navController显示的控制器中调用此函数,而不是在navController本身中调用。
显示:
[self.navigationItem.rightBarButtonItem.customView setAlpha:1.0];
隐藏:
[self.navigationItem.rightBarButtonItem.customView setAlpha:0.0];
你甚至可以为其显示/隐藏添加动画效果。[UIView animateWithDuration:0.2 animations:^{
[self.navigationItem.rightBarButtonItem.customView setAlpha:1.0];
}];
如果您在右侧有多个导航栏按钮,例如,如果您在导航栏右侧有两个按钮(搜索按钮和筛选按钮)。此时,右侧导航栏的按钮是self.navigationItem.rightBarButtonItem = nil;
如果您只想隐藏搜索按钮,可以使用以下代码行:self.navigationItem.rightBarButtonItems = [searchItem,filterItem]
这样,您就可以完全隐藏导航栏中的搜索按钮,并将筛选按钮放置在搜索按钮的位置。self.navigationItem.rightBarButtonItems = [filterItem]
这是Matt的解决方案,更新了故事板和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
这个答案的功劳要归功于 learner,该答案来源于以下问题:
这是一个更简单的答案。
//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
我尝试了所有其他方法,都没有成功,即使将我的按钮引用为@property (...) UIBarButtonItem....
,也没有用,直到我发现这个方法。
SWIFT 2.2
在 Swift 2.2 中,self.navigationItem 不起作用。相反,创建一个 NavigationItem 的输出口(我在下面命名为“nav”),并使用它。
此外,在 Xcode 7.3 和 Swift 2.2 中,以下建议对我无效:
nav.leftBarButtonItem?.customView?.hidden = true
所以我按照上面@Matt J的想法进行了操作(我左边有2个项目):
Create outlets for the items in the navigation bar and variables to store them
@IBOutlet weak var settingItem: UIBarButtonItem!
@IBOutlet weak var logoItem: UIBarButtonItem!
var sav_settingItem: UIBarButtonItem = UIBarButtonItem()
var sav_logoItem: UIBarButtonItem = UIBarButtonItem()
Save the items in viewDidLoad()
sav_settingItem = nav.leftBarButtonItems![0]
sav_logoItem = nav.leftBarButtonItems![1]
To HIDE set them to nil
nav.leftBarButtonItem = nil
nav.leftBarButtonItem = nil
To SHOW them
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];
我的解决方案:
self.navigationItem.rightBarButtonItem.customView.hidden=NO;