Disabled UIButton not faded or grey

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

提交回复
热议问题