UIButton tintColor for disabled and enabled state?

uibutton disabled color
uibutton background color for state
uibutton disable tint color
uibutton change background color when pressed swift
what is tintcolor
uibutton set disabled text color
uibutton set background color objective-c
uibutton image color

I have a custom class that inherits from UIButton. The thing that I want to accomplish is setting the tintColor property based on the button's enabled state (i.e enabled or disabled).

Is there any way to achieve that?

This is my class:

class ButtonsPostMenu: UIButton
{
    override func awakeFromNib()
    {
        titleLabel?.font = UIFont(name: Font_AvenirNext_Medium, size: 14)
        tintColor = UIColor.white
    }
}

You could override the isEnabled property to achieve that. The tintColor will be automatically changed according to the button's isEnabled status:

class ButtonsPostMenu:UIButton {

    //......

    override var isEnabled: Bool {
        didSet{
            if self.isEnabled {
                self.tintColor = UIColor.white
            }
            else{
                self.tintColor = UIColor.gray
            }
        }
    }

    //......

}

UIButton tintColor for disabled and enabled state?, The thing that I want to accomplish is setting the tintColor property based on the button's enabled state (i.e enabled or disabled). Is there any way to achieve that� All subclasses of UIView derive their behavior for tint Color from the base class. See the discussion of tint Color at the UIView level for more information.. This property has no default effect for buttons with type UIButton.Button Type.custom.

This is your class: Add changeStateOfButton custom method for manage tint color of UIButton

class ButtonsPostMenu: UIButton
{
    override func awakeFromNib()
    {
        titleLabel?.font = UIFont(name: Font_AvenirNext_Medium, size: 14)
        tintColor = UIColor.white
    }

    func changeStateOfButton() {
       if self.isEnabled {
           self.tintColor = UIColor.red // Set your color when button is enabled 
       }
       else {
           self.tintColor = UIColor.yellow // Set your color when button is disabled
       }
  }
}

And just call above method like when you want to set color based on enable/disable UIButton.

tintColor, This property has no default effect for buttons with type UIButton.ButtonType. custom . A Boolean value that determines whether the image changes when the button is disabled. Returns the background image used for a button state. When the Highlighted Adjusts Image (adjusts Image When Highlighted) option is enabled, button images get darker when it is in the highlighted state. When the Disabled Adjusts Image ( adjusts Image When Disabled ) option is enabled, the image is dimmed when the button is disabled.

    func setBackgroundColor(color: UIColor, forState: UIControlState) {
UIGraphicsBeginImageContext(CGSize(width: 1, height: 1))
CGContextSetFillColorWithColor(UIGraphicsGetCurrentContext(), color.CGColor)
CGContextFillRect(UIGraphicsGetCurrentContext(), CGRect(x: 0, y: 0, width: 1, height: 1))
let colorImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()

self.setBackgroundImage(colorImage, forState: forState)
 }

Extending UIButton in Swift with Background Color for State, Swift Tool Belt, Part 8: Extending UIButton with Background Color for State A UIButton can be in many states: default, selected, highlighted, or disabled. Our extension can't add a property because that is not allowed on� Oh, you're not talking about the label of the button, but the enabled state. My typical approach here is to have the state be a boolean value, and name the attribute the positive form of the state. I.e., if Button.enabled=true, then we know the button is enabled. If Button.enabled=false, then we know the button is disabled.

I think image filtering is a good option.

btn.setImage(image, for: UIControl.State.normal)
btn.setImage(image.disabled, for: UIControl.State.disabled)

Use CoreImage to do image filter

extension UIImage{
    var disabled: UIImage?{
        let ciImage = CIImage(image: self)
        let grayscale = ciImage?.applyingFilter("CIColorControls",
                                                parameters: [ kCIInputSaturationKey: 0.0 ])
        if let gray = grayscale{
            return UIImage(ciImage: gray)
        }
        else{
            return nil
        }
    }
}

Of course, in Swift 5

Disabled UIButton not faded or grey, uibutton tintcolor for state uibutton set selected state programmatically Button changes title color when the button is disabled/re-enabled , Note the enabled flat � In this tutorial, you will learn how to create UIButton programmatically. You will also learn how to do the basic customization of UIButton like setting button tint color, background color and making your button call a function when tapped. Create UIButton A new UIButton can be created of different types: ButtonType.custom, ButtonType.system, ButtonType.detailDisclosure, ButtonType.infoLight

You can attempt to replicate the setX(_ x: X, forState: State) API like this:

private class TintColorButton: UIButton {

    private var tintDict: [State.RawValue: UIColor] = [:]
    private var store: Set<AnyCancellable> = []

    override init(frame: CGRect) {
        super.init(frame: frame)

        publisher(for: \.isHighlighted).sink { [weak self] _ in self?.applyTintColor() }.store(in: &store)
        publisher(for: \.isEnabled).sink { [weak self] _ in self?.applyTintColor() }.store(in: &store)
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func setTintColor(_ color: UIColor?, forState state: State) {
        tintDict[state.rawValue] = color
        applyTintColor()
    }

    private func applyTintColor() {
        tintColor = tintDict[state.rawValue] ?? tintDict[State.normal.rawValue]
    }
}

rdar://49639040: UIButton tint color for state support, UIButton would benefit greatly from proper support for different tint If this were possible, it would enable designs where the image tint color follows the title Disabled state: Gray background image, white title and white icon. The background image to display when the text field is enabled. This image is displayed behind the rest of the text field’s content. To set this attribute programmatically, use the background property. Disabled. The background image to display when the text field is disabled. This image is displayed behind the rest of the text field’s content.

Why You Shouldn't Gray Out Disabled Buttons, Although the button is faded out, some color still bleeds through for recognition. As the disabled button transitions to an enabled state, its new� In this Swift code example we will learn how to disable UIButton. Create new UIButton Position button within a view Set UIButton title Set UIButton background colour Set button tint colour Add button target function Disable UIButton Add UIButton to a UIView as subview import UIKit class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad()…

Changing UIButton background color based on state and the color , It's a simple button for adding a page to your favorites. When favorited, I set the state to "Selected" and change the text and color. When not … Seems nobody here has mentioned using Key Value Observation yet, but it's another approach.. A reason for doing so instead of picking the other answers here is you don't need to go creating new images all the time nor be concerned with secondary effects of assigning images to buttons (e.g. cornerRadius effects).

Button changes title color when the button is disabled/re-enabled , Open Catalog > Buttons > Demo. => Note the enabled flat button title color is grey . Tap a button (it spawns a form sheet that is automatically� enabled: Enabled Behaviours are Updated, disabled Behaviours are not. isActiveAndEnabled: Has the Behaviour had active and enabled called? gameObject: The game object this component is attached to. A component is always attached to a game object. tag: The tag of this game object. transform: The Transform attached to this GameObject. runInEditMode

Comments
  • Not sure how to do that programmatically though: How did you accomplish that differently?
  • @EridB - I chose my words poorly. Ignore that.
  • Also used this to change tintColor for highlighted state by overriding isHighlighted