How do I hide/show the right button in the Navigation Bar

前端 未结 18 1939
后悔当初
后悔当初 2020-12-12 19:25

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:



        
相关标签:
18条回答
  • 2020-12-12 19:38

    Show:

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

    Hide:

    [self.navigationItem.rightBarButtonItem.customView setHidden:YES];
    
    0 讨论(0)
  • 2020-12-12 19:39

    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];
    
        }];
    
    0 讨论(0)
  • 2020-12-12 19:49

    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"
    }
    
    0 讨论(0)
  • 2020-12-12 19:50

    For swift 5 to hide rightBarButtonItem

    self.navigationItem.rightBarButtonItem?.customView?.isHidden = true
    
    0 讨论(0)
  • 2020-12-12 19:51

    Show:

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

    Hide:

    //set navigationItem tint clear white
    self.navigationItem.rightBarButtonItem.tintColor = [UIColor clearColor];
    
    0 讨论(0)
  • 2020-12-12 19:51
    1. Assume you can reference the specific bar button as variable 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]

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

    2. Show xxxButton.customView = nil or navigationItem.rightBarButtonItems?.insert(newElement: xxxButton, at:SOME_INDEX)

    Hope helpful.

    0 讨论(0)
提交回复
热议问题