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:
我在Storyboard中设置了按钮,如下所示:
self.navigationController.navigationItem.rightBarButtonItem.enabled = NO;
What do I need to do to change the button`s state visually too?
我还需要做什么来直观地改变按钮状态?
Thank you!
5 个解决方案
#1
5
Credit to Vijay-Apple-Dev.blogspo for this answer.
感谢Vijay-Apple-Dev.blogspo这个答案。
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.
注意:问题中的假设是self.navigationController.navigationItem.rightBarButtonItem.enabled = NO;工作似乎是错误的,在我的代码中试用它后我发现它没有任何效果。
Again thanks to Vijay noting that it might be better to use:
再次感谢Vijay注意到使用它可能更好:
self.navigationItem.hidesBackButton = YES;
As Apple doesn't like disabling the back button.
由于Apple不喜欢禁用后退按钮。
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.
发布此答案是为了完整性,因此未来的用户无需继续搜索此页面。
#2
5
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
#3
1
try something like this:
尝试这样的事情:
[[self.navigationItem.rightBarButtonItems objectAtIndex:0] setEnabled:NO];
#4
0
You can try to manually change the tint colour like this:
您可以尝试手动更改色调颜色,如下所示:
self.navigationItem.rightBarButtonItem.tintColor = [UIColor grayColor];
This is untested code, but it should work. Also, don't forget to change it back when it's enabled again.
这是未经测试的代码,但它应该可以工作。此外,不要忘记在再次启用时将其更改回来。
#5
0
Update it to:
将其更新为:
self.navigationItem.leftBarButtonItem?.isEnabled = true
#1
5
Credit to Vijay-Apple-Dev.blogspo for this answer.
感谢Vijay-Apple-Dev.blogspo这个答案。
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.
注意:问题中的假设是self.navigationController.navigationItem.rightBarButtonItem.enabled = NO;工作似乎是错误的,在我的代码中试用它后我发现它没有任何效果。
Again thanks to Vijay noting that it might be better to use:
再次感谢Vijay注意到使用它可能更好:
self.navigationItem.hidesBackButton = YES;
As Apple doesn't like disabling the back button.
由于Apple不喜欢禁用后退按钮。
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.
发布此答案是为了完整性,因此未来的用户无需继续搜索此页面。
#2
5
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
#3
1
try something like this:
尝试这样的事情:
[[self.navigationItem.rightBarButtonItems objectAtIndex:0] setEnabled:NO];
#4
0
You can try to manually change the tint colour like this:
您可以尝试手动更改色调颜色,如下所示:
self.navigationItem.rightBarButtonItem.tintColor = [UIColor grayColor];
This is untested code, but it should work. Also, don't forget to change it back when it's enabled again.
这是未经测试的代码,但它应该可以工作。此外,不要忘记在再次启用时将其更改回来。
#5
0
Update it to:
将其更新为:
self.navigationItem.leftBarButtonItem?.isEnabled = true