Disabled UIButton not faded or grey

后端 未结 19 1568
死守一世寂寞
死守一世寂寞 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: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!!!

提交回复
热议问题