How do I show/hide a UIBarButtonItem?

前端 未结 30 2156
执念已碎
执念已碎 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

    For Swift 3 and Swift 4 you can do this to hide the UIBarButtomItem:

    self.deleteButton.isEnabled = false
    self.deleteButton.tintColor = UIColor.clear
    

    And to show the UIBarButtonItem:

    self.deleteButton.isEnabled = true
    self.deleteButton.tintColor = UIColor.blue
    

    On the tintColor you must have to specify the origin color you are using for the UIBarButtomItem

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-28 01:57

    Try in Swift, don't update the tintColor if you have some design for your UIBarButtonItem like font size in AppDelegate, it will totally change the appearance of your button when showing up.

    In case of a text button, changing title can let your button 'disappear'.

    if WANT_TO_SHOW {
        myBarButtonItem.enabled = true
        myBarButtonItem.title = "BUTTON_NAME"
    }else{
        myBarButtonItem.enabled = false
        myBarButtonItem.title = ""
    }
    
    0 讨论(0)
  • 2020-11-28 01:57

    Here is an extension that will handle this.

    extension UIBarButtonItem {
    
        var isHidden: Bool {
            get {
                return tintColor == .clear
            }
            set {
                tintColor = newValue ? .clear : .white //or whatever color you want
                isEnabled = !newValue
                isAccessibilityElement = !newValue
            }
        }
    
    }
    

    USAGE:

    myBarButtonItem.isHidden = true
    
    0 讨论(0)
  • 2020-11-28 02:00

    iOS 8. UIBarButtonItem with custom image. Tried many different ways, most of them were not helping. Max's solution, thesetTintColor was not changing to any color. I figured out this one myself, thought it will be of use to some one.

    For Hiding:

    [self.navigationItem.rightBarButtonItem setEnabled:NO];
    [self.navigationItem.rightBarButtonItem setImage:nil];
    

    For Showing:

    [self.navigationItem.rightBarButtonItem setEnabled:YES];
    [self.navigationItem.rightBarButtonItem setImage:image];
    
    0 讨论(0)
  • 2020-11-28 02:00

    You need to manipulate the toolbar.items array.

    Here is some code I use to hide and display a Done button. If your button is on the extreme edge of the toolbar or in-between other buttons your other buttons will move, so if you want your button to just disappear then place your button as the last button towards the centre. I animate the button move for effect, I quite like it.

    -(void)initLibraryToolbar {
    
        libraryToolbarDocumentManagementEnabled = [NSMutableArray   arrayWithCapacity:self.libraryToolbar.items.count];
        libraryToolbarDocumentManagementDisabled = [NSMutableArray arrayWithCapacity:self.libraryToolbar.items.count];
        [libraryToolbarDocumentManagementEnabled addObjectsFromArray:self.libraryToolbar.items];
        [libraryToolbarDocumentManagementDisabled addObjectsFromArray:self.libraryToolbar.items];
        trashCan = [libraryToolbarDocumentManagementDisabled objectAtIndex:3];
        mail = [libraryToolbarDocumentManagementDisabled objectAtIndex:5];
        [libraryToolbarDocumentManagementDisabled removeObjectAtIndex:1];
        trashCan.enabled = NO;
        mail.enabled = NO;
        [self.libraryToolbar setItems:libraryToolbarDocumentManagementDisabled animated:NO];
    

    }

    so now can use the following code to show your button

    [self.libraryToolbar setItems:libraryToolbarDocumentManagementEnabled animated:YES];
    trashCan.enabled = YES;
    mail.enabled = YES; 
    

    or to hide your button

    [self.libraryToolbar setItems:libraryToolbarDocumentManagementDisabled animated:YES];
    trashCan.enabled = NO;
    mail.enabled = NO;
    
    0 讨论(0)
提交回复
热议问题