Disabled UIButton not faded or grey

117,467

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

enter image description here

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.

Share:
117,467

Related videos on Youtube

Andy A
Author by

Andy A

A software engineer with experience in Java, Hibernate, Android and Cocoa-touch.

Updated on October 27, 2021

Comments

  • Andy A
    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
      Jhaliya - Praveen Sharma about 13 years
      Ary you using image with your UIButton ?
    • Ravin
      Ravin about 13 years
      No, it doesn't,you will need to set its alpha, accordingly, it will work.
  • nielsbot
    nielsbot almost 11 years
    he'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
    Phantômaxx over 9 years
    Some description accompanying your code would make for a better answer.
  • Benjohn
    Benjohn almost 9 years
    This 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
    Benjohn almost 9 years
    While this works I reckon solutions that change the look depending on control state like this and this from below are a much better idea. They're using the built in mechanism to achieve an automatic change, rather than needing to explicitly update the control's look depending on its state.
  • Zippy
    Zippy about 8 years
    What does that do? Can you explain why should he use that?
  • Hunter Monk
    Hunter Monk over 7 years
    This only changes properties in the Button sub heading. Background color is under the View subheading, so isn't changed by state configs.
  • Anton
    Anton about 7 years
    This is not working for view's background color. In my project I combined previous answer and this.
  • atereshkov
    atereshkov almost 6 years
    Yep, seems to be a working solution. I just use the light gray color.
  • Paulo Cesar
    Paulo Cesar over 5 years
    This should be the accepted answer. On Swift you will need something like setTitleColor(disabledColor, for: [.disabled, .selected])
  • Ali Hus
    Ali Hus almost 5 years
    This 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
    qu1j0t3 about 3 years
    I think OP has the opposite problem. They are already changing this flag but want the appearance to change.
  • Sri Harsha Chilakapati
    Sri Harsha Chilakapati about 3 years
    Why is a DispatchQueue needed here? Isn't isEnabled itself a main thread only operation in the first place?