Disabled UIButton not faded or grey
Solution 1
You can use following code:
sendButton.enabled = YES;
sendButton.alpha = 1.0;
or
sendButton.enabled = NO;
sendButton.alpha = 0.5;
Solution 2
just change state config to disable and choose what you want, background Image for disabled state
Solution 3
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.
In code, use the -setTitleColor:forState:
message.
Solution 4
To make the button is faded when disable, you can set alpha for it. There are two options for you:
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 {
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:
class MyButton: UIButton {
override var isEnabled: Bool {
didSet {
alpha = isEnabled ? 1.0 : 0.5
}
}
}
Solution 5
Try to set the different images for UIControlStateDisabled
(disabled gray image) and UIControlStateNormal
(Normal image) so the button generate the disabled state for you.
Related videos on Youtube
Andy A
A software engineer with experience in Java, Hibernate, Android and Cocoa-touch.
Updated on October 27, 2021Comments
-
Andy A over 2 years
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 ...
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?
-
Jhaliya - Praveen Sharma about 13 yearsAry you using image with your UIButton ?
-
Ravin about 13 yearsNo, it doesn't,you will need to set its alpha, accordingly, it will work.
-
-
nielsbot almost 11 yearshe's not using images--but you could apply this same concept to
[ UIButton setAttributedTitle:forState:]
. Create your attributed string where your text foreground color is set to a transparent color. -
Phantômaxx over 9 yearsSome description accompanying your code would make for a better answer.
-
Benjohn almost 9 yearsThis seems like a much better solution than the excepted answer of adjusting the alpha. I'd already voted this up months ago and now I'm back here with the same problem, I wish I could do it again!
-
Benjohn almost 9 years
-
Zippy about 8 yearsWhat does that do? Can you explain why should he use that?
-
Hunter Monk over 7 yearsThis only changes properties in the Button sub heading. Background color is under the View subheading, so isn't changed by state configs.
-
Anton about 7 yearsThis is not working for view's background color. In my project I combined previous answer and this.
-
atereshkov almost 6 yearsYep, seems to be a working solution. I just use the light gray color.
-
Paulo Cesar over 5 yearsThis should be the accepted answer. On Swift you will need something like setTitleColor(disabledColor, for: [.disabled, .selected])
-
Ali Hus almost 5 yearsThis should be the accepted answer for a text button.. Current accepted answer is good for an image button. What I get from op's comments is that he has a text button..
-
qu1j0t3 about 3 yearsI think OP has the opposite problem. They are already changing this flag but want the appearance to change.
-
Sri Harsha Chilakapati about 3 yearsWhy is a DispatchQueue needed here? Isn't isEnabled itself a main thread only operation in the first place?