How can I dynamically hide a button from a view?

前端 未结 12 2102
执念已碎
执念已碎 2020-12-04 21:24

I would like to dynamically hide a button in one of my views depending on a certain condition.

I tried adding some code to the view controller\'s -viewWillAppe

相关标签:
12条回答
  • 2020-12-04 22:04

    This answer is regarding text-based UIBarButtonItems, however, the same concept could be applied to other types of buttons as well. Note that this will allow one to both hide and show the item again. Many of the answers above (those setting the button's value to nil, for example, do not allow the button to be re-shown if that's desired).

    TL;DR:

    if (shouldShowMyBarButtonItem) {
        self.myBarButtonItem.title = nil;
        self.myBarButtonItem.action = nil;
    } else if (!shouldShowMyBarButtonItem) {
        self.myBarButtonItem.title = @"Title";
        self.myBarButtonItem.action = @selector(mySelector:);
    }
    

    The long version:

    The UIBarButtonItem I was trying to hide is in a UIToolbar, not a UINavigationBar so all the suggestions that access the left (or right) barButtonItem properties on the navigation item don't work for me. Also, as stated above, I wished to re-show the button when circumstances change.

    Michael's suggestion came closest to working, however, with iOS 7 at least, there was still a very small sliver of the button displayed that was tappable. In my app, tapping the item when it's not supposed to be available was unacceptable. The above code both hides and, crucially, deactivates the button.

    I call the above code in a private refresh method, which is called when a user event occurs.

    0 讨论(0)
  • 2020-12-04 22:10

    This is a bit of a hack, but it works in my case (and it properly handles dynamic spacing):

    To hide:

    myButton.width = 0.1;
    

    To show:

    myButton.width = 0.0;
    

    A width of 0.0 is "auto width", and with a width of 0.1, the button totally disappears (not even a "sliver" of a button, though I haven't tried this on a retina display).

    0 讨论(0)
  • 2020-12-04 22:11

    If you add a UIButton to the UIBarButtonItem rather than just use the UIBarButtonItem.

    You can then assign UIButton.hidden to TRUE or YES and it (and the UIBarButtonItem) will not be visible or active.

    Hope that helps.

    0 讨论(0)
  • 2020-12-04 22:12

    If all that one is trying to hide is the back button in the navigation bar, there is an easier way:

    self.navigationItem.hidesBackButton = YES;
    

    Quote from developer documentation:

    hidesBackButton

    A Boolean value that determines whether the back button is hidden.

    @property(nonatomic, assign) BOOL hidesBackButton

    Discussion

    YES if the back button is hidden when this navigation item is the top item; otherwise, NO. The default value is NO.

    Availability

    Available in iPhone OS 2.0 and later.

    0 讨论(0)
  • 2020-12-04 22:18

    This is what I did for button items that weren't part of the navigation bar (where Blank.png is a blank image I created that's the same size of the image it replaces):

    theButton.enabled = NO;
    theButton.image = [UIImage imageNamed: @"Blank.png"];
    
    0 讨论(0)
  • 2020-12-04 22:18

    Just set the button's hidden property to true:

    myButton.hidden = YES;
    
    0 讨论(0)
提交回复
热议问题