Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disabling a Navigation Bar Button Item

When setting the right navigation bar button to disabled, it does not change its color to signal its disabled state to the user.

It remains "highlighted", non-greyed-out.

I have set up the button in Storyboard as follows:

enter image description here

self.navigationController.navigationItem.rightBarButtonItem.enabled = NO;

What do I need to do to change the button`s state visually too?

Thank you!

like image 854
AlexR Avatar asked Jun 25 '13 14:06

AlexR


3 Answers

Update it to:

self.navigationItem.leftBarButtonItem?.isEnabled = true
like image 117
Anubz Avatar answered Oct 05 '22 17:10

Anubz


Credit to Vijay-Apple-Dev.blogspo for this answer.

self.navigationItem.leftBarButtonItem.enabled = NO;
self.navigationItem.rightBarButtonItem.enabled = NO;

This automatically seems to grey out the buttons and also disables them.

NOTE: The assumption in the question that self.navigationController.navigationItem.rightBarButtonItem.enabled = NO; works seems to be wrong, after trialling it in my code I found it had no effect.

Again thanks to Vijay noting that it might be better to use:

self.navigationItem.hidesBackButton = YES;

As Apple doesn't like disabling the back button.

Personally I am going to disable the top right button and hide the back button to stop the user from pressing them, then enable them again when I choose.

A link to the question is here

This answer is posted for completeness and so future users don't need to continue searching past this page.

like image 21
sam_smith Avatar answered Oct 05 '22 19:10

sam_smith


None of the relevant answers were able to produce the result I was looking for - but I was able to solve this by setting the text attributes on my navigation buttons:

[navButton setTitleTextAttributes:@{NSForegroundColorAttributeName:enabledColor} forState:UIControlStateNormal];
[navButton setTitleTextAttributes:@{NSForegroundColorAttributeName:disabledColor} forState:UIControlStateDisabled];

Then, all I need to do is the following and the user interactability and color changes automatically:

navButton.enabled = YES; // or NO
like image 26
Mike Avatar answered Oct 05 '22 19:10

Mike