Disabled UIButton not faded or grey

后端 未结 19 1440
死守一世寂寞
死守一世寂寞 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:38
    #import "UIButton+My.h"
    #import <QuartzCore/QuartzCore.h>
    @implementation UIButton (My)
    
    
    -(void)fade :(BOOL)enable{
        self.enabled=enable;//
        self.alpha=enable?1.0:0.5;
    }
    
    @end
    

    .h:

    #import <UIKit/UIKit.h>
    @interface UIButton (My)
    
    -(void)fade :(BOOL)enable;
    
    @end
    
    0 讨论(0)
  • 2020-12-04 16:39

    In Swift:

    previousCustomButton.enabled = false
    previousCustomButton.alpha = 0.5
    

    or

    nextCustomButton.enabled = true
    nextCustomButton.alpha = 1.0
    
    0 讨论(0)
  • 2020-12-04 16:39

    It looks like the following code works fine. But in some cases, this doesn't work.

    sendButton.enabled = NO;
    sendButton.alpha = 0.5;
    

    If the above code doesn't work, please wrap it in Main Thread. so

    dispatch_async(dispatch_get_main_queue(), ^{
        sendButton.enabled = NO;
        sendButton.alpha = 0.5
    });
    

    if you go with swift, like this

    DispatchQueue.main.async {
        sendButton.isEnabled = false
        sendButton.alpha = 0.5
    }
    

    In addition, if you customized UIButton, add this to the Button class.

    override var isEnabled: Bool {
        didSet {
            DispatchQueue.main.async {
                if self.isEnabled {
                    self.alpha = 1.0
                }
                else {
                    self.alpha = 0.5
                }
            }
        }
    }
    

    Thank you and enjoy coding!!!

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

    You can use adjustsImageWhenDisabled which is property of UIButton
    (@property (nonatomic) BOOL adjustsImageWhenDisabled)

    Ex:

     Button.adjustsImageWhenDisabled = false
    
    0 讨论(0)
  • 2020-12-04 16:47

    just change state config to disable and choose what you want, background Image for disabled state

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

    Try to set the different images for UIControlStateDisabled (disabled gray image) and UIControlStateNormal(Normal image) so the button generate the disabled state for you.

    0 讨论(0)
提交回复
热议问题