How do I hide/show the right button in the Navigat

2019-01-16 11:17发布

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:

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

Is there a way?

15条回答
一夜七次
2楼-- · 2019-01-16 11:56

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];

    }];
查看更多
SAY GOODBYE
3楼-- · 2019-01-16 11:59

For Swift 3

if let button = self.navigationItem.rightBarButtonItem {
                    button.isEnabled = false
                    button.tintColor = UIColor.clear
                }`
查看更多
姐就是有狂的资本
4楼-- · 2019-01-16 11:59

Set reference to nil:

current_controller_in_navcontroller.navigationItem.rightBarButtonItem =  nil;

Also be sure to call this in the controller currently shown by the navController, not for the navController itself.

查看更多
劳资没心,怎么记你
5楼-- · 2019-01-16 11:59

In swift 4 I has a trick to show / hide right or left button:

Step 1: Create a IBOutlet button in view controller:

@IBOutlet var navigationItemButton: UIBarButtonItem!

Step 2: Create Hide button function:

func hideNavigationButton() {
    navigationItemButton.isEnabled = false
    navigationItemButton.tintColor = UIColor.clear
}

Step 3: Create Show button function:

func showNavigationButton() {
    navigationItemButton.isEnabled = true
    navigationItemButton.tintColor = UIColor.white
}

Step 4: Just call the functions that you want, use hideNavigationButton() to hide, and showNavigationButton() to show the button.

Regards!

查看更多
仙女界的扛把子
6楼-- · 2019-01-16 12:00

Show:

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

Hide:

[self.navigationItem.rightBarButtonItem.customView setHidden:YES];
查看更多
放我归山
7楼-- · 2019-01-16 12:01

My solution:

self.navigationItem.rightBarButtonItem.customView.hidden=NO;
查看更多
登录 后发表回答