How do I show/hide a UIBarButtonItem?

前端 未结 30 2311
执念已碎
执念已碎 2020-11-28 01:18

I created a toolbar in IB with several buttons. I would like to be able to hide/show one of the buttons depending on the state of the data in the main window.

30条回答
  •  佛祖请我去吃肉
    2020-11-28 01:56

    Setting the text color to a clear color when the bar button item is disabled is probably a cleaner option. There's no weirdness that you have to explain in a comment. Also you don't destroy the button so you still keep any associated storyboard segues.

    [self.navigationItem.rightBarButtonItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor clearColor]}
                                                          forState:UIControlStateDisabled];
    

    Then when ever you want the bar button item hidden, you can just do:

    self.navigationItem.rightBarButton.enabled = NO;
    

    It's lame there's no hidden property but this offers the same result.

提交回复
热议问题