How to disable UIBarButtonItem?

后端 未结 4 2042
清酒与你
清酒与你 2020-12-30 18:53

I have a UIBarButtonItem that just doesn\'t want to get disabled. Short version: when I call

[myBarButtonItem setEnabled:NO];

Nothing happe

相关标签:
4条回答
  • 2020-12-30 19:32

    You can disable the left navigation button from inside an UIViewController like this, without using any IBOutlet:

    self.navigationItem.leftBarButtonItem.enabled = NO;
    

    To disable the right navigation button:

    self.navigationItem.rightBarButtonItem.enabled = NO;
    

    Swift3

    self.navigationItem.rightBarButtonItem?.isEnabled = false
    
    0 讨论(0)
  • 2020-12-30 19:38

    If your toolbar has an IBOutlet (and you've checked to make sure it's non-nil), try:

    [ [ [ myToolBar items ] objectAtIndex: myBarButtonItemIndex ] setEnabled:(NO) ];
    
    0 讨论(0)
  • 2020-12-30 19:38

    I used a different solution (Swift 4.2) for my rightBarButtonItems.

    I had 3 buttons so used a for loop, then made an extension of UINavigationItem so I could use it throughout my app.

    extension UINavigationItem {
    func setRightBarButtonItems(isEnabled:Bool){
        for button in self.rightBarButtonItems ?? [UIBarButtonItem()] {
            button.isEnabled = isEnabled
        }
    }
    

    Then i can call it from my TableViewController

    navigationItem.setRightBarButtonItems(isEnabled: false)
    
    0 讨论(0)
  • 2020-12-30 19:38

    In my case (Swift) I had 2 barButtonItems added as an array - so to disable them this did the trick

        navigationItem.rightBarButtonItems?.first?.enabled = false
        navigationItem.rightBarButtonItems?.last?.enabled = false
    
    0 讨论(0)
提交回复
热议问题