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

前端 未结 18 1941
后悔当初
后悔当初 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:59

    For Swift 3

    if let button = self.navigationItem.rightBarButtonItem {
                        button.isEnabled = false
                        button.tintColor = UIColor.clear
                    }`
    
    0 讨论(0)
  • 2020-12-12 20:00

    To hide:

    if let topItem = self.navigationController?.navigationBar.topItem {
        topItem.rightBarButtonItem = nil
    }
    
    0 讨论(0)
  • 2020-12-12 20:01

    If you have only one bar button item in the right side you can use this one,

    self.navigationItem.rightBarButtonItem = nil;

    Suppose if you have multiple bar button in the right side, for example suppose you have two bar button items(search button and filter button) in the right side of your navigation item. Now the right bar button items are

    self.navigationItem.rightBarButtonItems = [searchItem,filterItem]

    and you have to hide only search button, you can use like,

    self.navigationItem.rightBarButtonItems = [filterItem]

    Now what happening is, you can completely hide the search button from the navigation item and the filter item comes in the place of search item

    0 讨论(0)
  • 2020-12-12 20:02

    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!

    0 讨论(0)
  • 2020-12-12 20:05

    SWIFT 2.2

    In swift 2.2 self.navigationItem does not work. Instead create an outlet of the NavigationItem (I named it below "nav") and use it.

    Also the following suggestion did not work for me using Xcode 7.3 and swift 2.2

     nav.leftBarButtonItem?.customView?.hidden = true
    

    So I used the idea of @Matt J above as follows (I have 2 items on the left):

    1. 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()
      
    2. Save the items in viewDidLoad()

      sav_settingItem = nav.leftBarButtonItems![0]
      sav_logoItem = nav.leftBarButtonItems![1]
      
    3. To HIDE set them to nil

      nav.leftBarButtonItem = nil
      nav.leftBarButtonItem = nil
      
    4. To SHOW them

      if (nav.leftBarButtonItem == nil) {
          nav.leftBarButtonItem = sav_settingItem
          nav.leftBarButtonItems?.append(sav_logoItem)
          return
      }
      
    0 讨论(0)
  • 2020-12-12 20:05

    You can use below code:

        self.navigationItem.rightBarButtonItem?.image = nil
        self.navigationItem.rightBarButtonItem?.isEnabled = false
    
    0 讨论(0)
提交回复
热议问题