ios 禁用的 UIButton 不褪色或变灰
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5715092/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Disabled UIButton not faded or grey
提问by Andy A
In my iPhone app, I have a UIButton which I have created in Interface Builder. I can successfully enable and disable it like this in my code ...
在我的 iPhone 应用程序中,我有一个在 Interface Builder 中创建的 UIButton。我可以在我的代码中像这样成功启用和禁用它......
sendButton.enabled = YES;
or
或者
sendButton.enabled = NO;
However, the visual look of the button is always the same! It is not faded or grey. If I attempt to click it though, it is enabled or disabled as expected. Am I missing something? Shouldn't it look faded or grey?
但是,按钮的视觉外观始终相同!它没有褪色或灰色。如果我尝试单击它,它会按预期启用或禁用。我错过了什么吗?它不应该看起来褪色或灰色吗?
回答by Ravin
You can use following code:
您可以使用以下代码:
sendButton.enabled = YES;
sendButton.alpha = 1.0;
or
sendButton.enabled = NO;
sendButton.alpha = 0.5;
回答by Ahmed R.
回答by guywithmazda
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. Use the Text Color
popup button to change the text color.
在故事板编辑器中,Disabled
从State Config
弹出按钮中进行选择。使用Text Color
弹出按钮更改文本颜色。
In code, use the -setTitleColor:forState:
message.
在代码中,使用-setTitleColor:forState:
消息。
回答by Jhaliya
Try to set the different images for UIControlStateDisabled
(disabled gray image) and UIControlStateNormal
(Normal image) so the button generate the disabled state for you.
尝试为UIControlStateDisabled
(禁用灰度图像)和UIControlStateNormal
(普通图像)设置不同的图像,以便按钮为您生成禁用状态。
回答by Heo ??t Hades
To make the button is faded when disable, you can set alpha for it. There are two options for you:
要使按钮在禁用时褪色,您可以为其设置 alpha。有两种选择:
First way: If you want to apply for all your buttons in your app, so you can write extension
for UIButton like this:
第一种方式:如果你想为你的应用程序中的所有按钮申请,那么你可以extension
像这样为 UIButton编写:
extension UIButton {
open override var isEnabled: Bool{
didSet {
alpha = isEnabled ? 1.0 : 0.5
}
}
}
Second way: If you just want to apply for some buttons in your app, so you can write a custom class from UIButton like below and use this class for which you want to apply:
第二种方式:如果您只想在应用程序中申请某些按钮,那么您可以从 UIButton 编写一个自定义类,如下所示,并使用您要申请的此类:
class MyButton: UIButton {
override var isEnabled: Bool {
didSet {
alpha = isEnabled ? 1.0 : 0.5
}
}
}
回答by Antonio Moro
If you use a text button, you can put into viewDidLoad the instance method
如果使用文本按钮,则可以放入 viewDidLoad 实例方法
- (void)setTitleColor:(UIColor *)color forState:(UIControlState)state
example:
例子:
[self.syncImagesButton setTitleColor:[UIColor grayColor] forState:UIControlStateDisabled];
回答by Mahesh Lad
You can use adjustsImageWhenDisabled
which is property of UIButton
(@property (nonatomic) BOOL adjustsImageWhenDisabled)
您可以使用adjustsImageWhenDisabled
which 是的属性UIButton
(@property (nonatomic) BOOL adjustsImageWhenDisabled)
Ex:
前任:
Button.adjustsImageWhenDisabled = false
回答by Diego A. Rincon
This is quite an old question but there's a much simple way of achieving this.
这是一个相当古老的问题,但有一种非常简单的方法可以实现这一目标。
myButton.userInteractionEnabled = false
myButton.userInteractionEnabled = false
This will only disable any touch gestures without changing the appearance of the button
这只会禁用任何触摸手势而不改变按钮的外观
回答by King-Wizard
In Swift:
在斯威夫特:
previousCustomButton.enabled = false
previousCustomButton.alpha = 0.5
or
或者
nextCustomButton.enabled = true
nextCustomButton.alpha = 1.0
回答by Kiran S
Set title color
for different states:
设置title color
为不同的状态:
@IBOutlet weak var loginButton: UIButton! {
didSet {
loginButton.setTitleColor(UIColor.init(white: 1, alpha: 0.3), for: .disabled)
loginButton.setTitleColor(UIColor.init(white: 1, alpha: 1), for: .normal)
}
}
Usage: (text color will get change automatically)
用法:(文字颜色会自动改变)
loginButton.isEnabled = false