Disabled UIButton not faded or grey

后端 未结 19 1437
死守一世寂寞
死守一世寂寞 2020-12-04 16:11

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.e         


        
相关标签:
19条回答
  • 2020-12-04 16:28

    Swift 4+

    extension UIButton {
        override open var isEnabled: Bool {
            didSet {
                DispatchQueue.main.async {
                    if self.isEnabled {
                        self.alpha = 1.0
                    }
                    else {
                        self.alpha = 0.6
                    }
                }
            }
        }
    }
    

    How to use

    myButton.isEnabled = false
    
    
    0 讨论(0)
  • 2020-12-04 16:28

    You can use the both first answers and is going to be a better result.

    In the attribute inspector (when you're selecting the button), change the State Config to Disabled to set the new Image that is going to appear when it is disabled (remove the marker in the Content->Enabled check to made it disabled).

    And when you change the state to enabled, the image will load the one from this state.

    Adding the alpha logic to this is a good detail.

    0 讨论(0)
  • 2020-12-04 16:29

    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
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-04 16:29

    Create an extension in Swift > 3.0 rather than each button by itself

    extension UIButton {
        override open var isEnabled : Bool {
            willSet{
                if newValue == false {
                    self.setTitleColor(UIColor.gray, for: UIControlState.disabled)
                }
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-04 16:32

    I am stuck on the same problem. Setting alpha is not what I want.

    UIButton has "background image" and "background color". For image, UIButton has a property as

    @property (nonatomic) BOOL adjustsImageWhenDisabled

    And background image is drawn lighter when the button is disabled. For background color, setting alpha, Ravin's solution, works fine.

    First, you have to check whether you have unchecked "disabled adjusts image" box under Utilities-Attributes.
    Then, check the background color for this button.

    (Hope it's helpful. This is an old post; things might have changed in Xcode).

    0 讨论(0)
  • 2020-12-04 16:32

    If the UIButton is in the combined state of selected and disabled, its state is UIControlStateSelected | UIControlStateDisabled.

    So its state needs to be adjusted like this:

    [button setTitleColor:color UIControlStateSelected | UIControlStateDisabled];
    [button setImage:image forState:UIControlStateSelected | UIControlStateDisabled];
    

    One approach is to subclass UIButton as follows:

    @implementation TTButton
    - (void)awakeFromNib {
       [super awakeFromNib];
    
        //! Fix behavior when `disabled && selected`
        //! Load settings in `xib` or `storyboard`, and apply it again.
        UIColor *color = [self titleColorForState:UIControlStateDisabled];
        [self setTitleColor:color forState:UIControlStateDisabled];
    
        UIImage *img = [self imageForState:UIControlStateDisabled];
        [self setImage:img forState:UIControlStateDisabled];
    }
    
    - (void)setTitleColor:(UIColor *)color forState:(UIControlState)state {
        [super setTitleColor:color forState:state];
    
        //
        if (UIControlStateDisabled == state) {
            [super setTitleColor:color forState:UIControlStateSelected | state];
            [super setTitleColor:color forState:UIControlStateHighlighted | state];
        }
    }
    
    - (void)setImage:(UIImage *)image forState:(UIControlState)state {
        [super setImage:image forState:state];
    
        if (UIControlStateDisabled == state) {
            [super setImage:image forState:UIControlStateSelected | state];
            [super setImage:image forState:UIControlStateHighlighted | state];
        }
    }
    
    @end
    
    0 讨论(0)
提交回复
热议问题