How do I show/hide a UIBarButtonItem?

前端 未结 30 2153
执念已碎
执念已碎 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 02:09

    Here's a simple approach:

    hide:  barbuttonItem.width = 0.01;
    show:  barbuttonItem.width = 0; //(0 defaults to normal button width, which is the width of the text)
    

    I just ran it on my retina iPad, and .01 is small enough for it to not show up.

    0 讨论(0)
  • 2020-11-28 02:09

    Some helper methods I thought I'd share based upon lnafziger's accepted answer as I have multiple toolbars and multiple buttons in each:

    -(void) hideToolbarItem:(UIBarButtonItem*) button inToolbar:(UIToolbar*) toolbar{
        NSMutableArray *toolbarButtons = [toolbar.items mutableCopy];
        [toolbarButtons removeObject:button];
        [toolbar setItems:toolbarButtons animated:NO];
    }
    
    -(void) showToolbarItem:(UIBarButtonItem*) button inToolbar:(UIToolbar*) toolbar atIndex:(int) index{
        NSMutableArray *toolbarButtons = [toolbar.items mutableCopy];
        if (![toolbarButtons containsObject:button]){
            [toolbarButtons insertObject:button atIndex:index];
            [self setToolbarItems:toolbarButtons animated:YES];
        }
    }
    
    0 讨论(0)
  • 2020-11-28 02:10

    You can easily get the view and hide it this way

    let view: UIView = barButtonItem.valueForKey("view") as! UIView
    view.hidden = true
    
    0 讨论(0)
  • 2020-11-28 02:11

    I am currently running OS X Yosemite Developer Preview 7 and Xcode 6 beta 6 targeting iOS 7.1 and following solution works fine for me:

    • Create outlet for UINavigationItemand UIBarButtonItems
    • Run following code to remove

      [self.navItem setRightBarButtonItem:nil];
      [self.navItem setLeftBarButtonItem:nil];
      
    • Run following codes to add buttons again

      [self.navItem setRightBarButtonItem:deleteItem];
      [self.navItem setLeftBarButtonItem:addItem];
      
    0 讨论(0)
  • 2020-11-28 02:12
    @IBDesignable class AttributedBarButtonItem: UIBarButtonItem {
    
        var isHidden: Bool = false {
    
            didSet {
    
                isEnabled = !isHidden
                tintColor = isHidden ? UIColor.clear : UIColor.black
            }
        }
    }
    

    And now simply change isHidden property.

    0 讨论(0)
  • 2020-11-28 02:13

    Improving From @lnafziger answer

    Save your Barbuttons in a strong outlet and do this to hide/show it:

    -(void) hideBarButtonItem :(UIBarButtonItem *)myButton {
        // Get the reference to the current toolbar buttons
        NSMutableArray *navBarBtns = [self.navigationItem.rightBarButtonItems mutableCopy];
    
        // This is how you remove the button from the toolbar and animate it
        [navBarBtns removeObject:myButton];
        [self.navigationItem setRightBarButtonItems:navBarBtns animated:YES];
    }
    
    
    -(void) showBarButtonItem :(UIBarButtonItem *)myButton {
        // Get the reference to the current toolbar buttons
        NSMutableArray *navBarBtns = [self.navigationItem.rightBarButtonItems mutableCopy];
    
        // This is how you add the button to the toolbar and animate it
        if (![navBarBtns containsObject:myButton]) {
            [navBarBtns addObject:myButton];
            [self.navigationItem setRightBarButtonItems:navBarBtns animated:YES];
        }
    }
    

    When ever required use below Function..

    [self showBarButtonItem:self.rightBarBtn1];
    [self hideBarButtonItem:self.rightBarBtn1];
    
    0 讨论(0)
提交回复
热议问题