使 UIButton 看起来禁用的任何其他选项?
我正在尝试使用以下代码使 UIButton 看起来已禁用:
btnYourButton.alpha = 0.6f;
btnYourButton.enabled = NO;
虽然启用了制作(当然也看起来已启用)
btnYourButton.alpha = 1.0f;
btnYourButton.enabled = YES;
,但没有任何方法可以执行这两个操作(使 UIButton 禁用/启用并且还使它看起来在一条语句中禁用/启用)?
I am trying the following code to make a UIButton look Disabled :
btnYourButton.alpha = 0.6f;
btnYourButton.enabled = NO;
While making is enabled(and of course to look enabled)
btnYourButton.alpha = 1.0f;
btnYourButton.enabled = YES;
Isn't there any way, by which I can perform both of this (making UIButton disable/enable and also to make it look disable/enable) in a single statement ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
或者您可以尝试子类化 UIButton,例如:
file MyKindOfButton.h
file MyKindOfButton.m
Or you could try subclassing UIButton, something like:
file MyKindOfButton.h
file MyKindOfButton.m
我知道这是一个非常古老的问题,但这是一个非常好的解决方案。只需创建一个 UIColor 类别并添加此方法即可。
现在您只需将背景图像设置为您想要的任何颜色,它就会自动为您处理禁用外观。
I know this is a very old question, but here is a very nice solution. Just create a category of UIColor and add this method.
Now you can just set the backgroundImage to whatever color you want and it will automatically handle the disable appearance for you.
另一种选择是更改禁用状态的文本颜色(例如,浅灰色)。在故事板编辑器中,从“状态配置”弹出按钮中选择“禁用”,然后使用“文本颜色”弹出按钮更改文本颜色。在代码中,使用 -setTitleColor:forState: 消息。
(我意识到这是一篇较旧的帖子,但我认为其他人可能会喜欢这个选项)
Another option is to change the text color (to light gray for example) for the disabled state. In the storyboard editor, choose Disabled from the State Config popup button, and use the Text Color popup button to change the text color. In code, use the -setTitleColor:forState: message.
(I realize this is an older post, but I thought others might like this option)