how to disable a navigation bar button item in iOS

前端 未结 16 1280
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-01 09:54

I have created a navigation controller. In the second view (which is pushed), I have some webservice call and placing a overlay view and setting

self.view.use

相关标签:
16条回答
  • 2021-01-01 10:08

    Swift 5 It's working for Navigation controller

    //Disable

    self.navigationController?.navigationBar.isUserInteractionEnabled = false
    

    //Enable

    self.navigationController?.navigationBar.isUserInteractionEnabled = true
    
    0 讨论(0)
  • 2021-01-01 10:10

    Updated for Swift 3:

    If you want to disable a navigation bar button item OR you want to disable hole UINavigationBar i.e. all item present on navigation bar, use below lines of code;

    // if you want disable
    self.navigationController?.navigationBar.isUserInteractionEnabled = false
    
    // if you want enable again
    self.navigationController?.navigationBar.isUserInteractionEnabled = true
    

    Enjoy...!

    0 讨论(0)
  • One line solution

    self.navigationItem.leftBarButtonItem.enabled = NO;
    
    self.navigationItem.rightBarButtonItem.enabled = NO;
    
    0 讨论(0)
  • 2021-01-01 10:15

    The simplest way to truly disable a UIBarButtonItem would be as followed:

    barButtonVar.isEnabled = false
    
    0 讨论(0)
  • 2021-01-01 10:16

    Navigation bar button items must be toggled by referring to them via the navigationItem property.

    For example:

    func setupNav() {
        let saveButton = UIBarButtonItem.init(barButtonSystemItem: .save, target: self, action: #selector(onSavePressed))
        navigationItem.rightBarButtonItem = saveButton
        saveButton.isEnabled = false
      }
    
    func validateSave() {
         saveButton.isEnabled = isConditionMet // WON'T work
          navigationItem.rightBarButtonItem.isEnabled = isConditionMet // WORKS!
    }
    
    0 讨论(0)
  • 2021-01-01 10:18

    For version iOS 10.3, swift 3:

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