Disabled UIButton not faded or grey

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

    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.

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

    Set title color for different states:

    @IBOutlet weak var loginButton: UIButton! {
            didSet {
                loginButton.setTitleColor(UIColor.init(white: 1, alpha: 0.3), for: .disabled)
                loginButton.setTitleColor(UIColor.init(white: 1, alpha: 1), for: .normal)
            }
        }
    

    Usage: (text color will get change automatically)

    loginButton.isEnabled = false
    

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

    This question has a lot of answers but all they looks not very useful in case if you really want to use backgroundColor to style your buttons. UIButton has nice option to set different images for different control states but there is not same feature for background colors. So one of solutions is to add extension which will generate images from color and apply them to button.

    extension UIButton {
      private func image(withColor color: UIColor) -> UIImage? {
        let rect = CGRect(x: 0.0, y: 0.0, width: 1.0, height: 1.0)
        UIGraphicsBeginImageContext(rect.size)
        let context = UIGraphicsGetCurrentContext()
    
        context?.setFillColor(color.cgColor)
        context?.fill(rect)
    
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
    
        return image
      }
    
      func setBackgroundColor(_ color: UIColor, for state: UIControlState) {
        self.setBackgroundImage(image(withColor: color), for: state)
      }
    }
    

    Only one issue with this solution -- this change won't be applied to buttons created in storyboard. As for me it's not an issue because I prefer to style UI from code. If you want to use storyboards then some additional magic with @IBInspectable needed.

    Second option is subclassing but I prefer to avoid this.

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

    Maybe you can subclass your button and override your isEnabled variable. The advantage is that you can reuse in multiple places.

    override var isEnabled: Bool {
         didSet {
             if isEnabled {
                 self.alpha = 1
             } else {
                 self.alpha = 0.2
             }
         }
     }
    
    0 讨论(0)
  • 2020-12-04 16:36

    This is quite an old question but there's a much simple way of achieving this.

    myButton.userInteractionEnabled = false

    This will only disable any touch gestures without changing the appearance of the button

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

    If you use a text button, you can put into viewDidLoad the instance method

    - (void)setTitleColor:(UIColor *)color forState:(UIControlState)state
    

    example:

    [self.syncImagesButton setTitleColor:[UIColor grayColor] forState:UIControlStateDisabled];
    
    0 讨论(0)
提交回复
热议问题