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.
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
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.
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 = ""
}
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
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];
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;