How to change the background color of a UIButton while it's highlighted?

uibutton background color swift
uibutton background color not changing swift
swift change button color when pressed
swift change background color programmatically
how to change background color in swift
uibutton remove selected background color
swift highlight button when pressed
uibutton selected and unselected swift

At some point in my app I have a highlighted UIButton (for example when a user has his finger on the button) and I need to change the background color while the button is highlighted (so while the finger of the user is still on the button).

I tried the following:

_button.backgroundColor = [UIColor redColor];

But it is not working. The color remains the same. I tried the same piece of code when the button is not highlighted and it works fine. I also tried calling -setNeedsDisplay after changing the color, it didn't have any effect.

How to force the button to change the background color?


You can override UIButton's setHighlighted method.

Objective-C

- (void)setHighlighted:(BOOL)highlighted {
    [super setHighlighted:highlighted];

    if (highlighted) {
        self.backgroundColor = UIColorFromRGB(0x387038);
    } else {
        self.backgroundColor = UIColorFromRGB(0x5bb75b);
    }
}

Swift 3.0 and Swift 4.1

override open var isHighlighted: Bool {
    didSet {
        backgroundColor = isHighlighted ? UIColor.black : UIColor.white
    }
}

Extending UIButton in Swift with Background Color for State, For instance, you can change the title, font, text color, shadow color, image, and background image, but not the background color. For some  At some point in my app I have a highlighted UIButton (for example when a user has his finger on the button) and I need to change the background color while the button is highlighted (so while the finger of the user is still on the button). I tried the following: _button.backgroundColor = [UIColor redColor]; But it is not working.


Not sure if this sort of solves what you're after, or fits with your general development landscape but the first thing I would try would be to change the background colour of the button on the touchDown event.

Option 1:

You would need two events to be capture, UIControlEventTouchDown would be for when the user presses the button. UIControlEventTouchUpInside and UIControlEventTouchUpOutside will be for when they release the button to return it to the normal state

UIButton *myButton =  [UIButton buttonWithType:UIButtonTypeCustom];
[myButton setFrame:CGRectMake(10.0f, 10.0f, 100.0f, 20.f)];
[myButton setBackgroundColor:[UIColor blueColor]];
[myButton setTitle:@"click me:" forState:UIControlStateNormal];
[myButton setTitle:@"changed" forState:UIControlStateHighlighted];
[myButton addTarget:self action:@selector(buttonHighlight:) forControlEvents:UIControlEventTouchDown];
[myButton addTarget:self action:@selector(buttonNormal:) forControlEvents:UIControlEventTouchUpInside];

Option 2:

Return an image made from the highlight colour you want. This could also be a category.

+ (UIImage *)imageWithColor:(UIColor *)color {
   CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
   UIGraphicsBeginImageContext(rect.size);
   CGContextRef context = UIGraphicsGetCurrentContext();

   CGContextSetFillColorWithColor(context, [color CGColor]);
   CGContextFillRect(context, rect);

   UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
   UIGraphicsEndImageContext();

   return image;
}

and then change the highlighted state of the button:

[myButton setBackgroundImage:[self imageWithColor:[UIColor greenColor]] forState:UIControlStateHighlighted];

How to change a button background color using Swift?, To change the background color of a button in iOS application we need to access the property ' backgroundColor' of the UIButton. We can UIButton. We can do this in two ways, programmatically and using the storyboard. I think it will help you to set background color and set background image for a button. If you want to set image please change the button type RoundedRect to Custom. I hope it will help you little bit. Thanks.


There is no need to override highlighted as computed property. You can use property observer to trigger background color change:

override var highlighted: Bool {
    didSet {
        backgroundColor = highlighted ? UIColor.lightGrayColor() : UIColor.whiteColor()
    }
}

Swift 4

override open var isHighlighted: Bool {
    didSet {
        backgroundColor = isHighlighted ? UIColor.lightGray : UIColor.white
    }
}

Changing UIButton background color based on state and the color , Now the switch to selected happens smoothly but when the button swaps back to normal, the background color change lags to the point where it is noticeably late. We already have a background color for UIButton that can be used for the default state, so that means we need to add a color to Interface Builder for the disabled, selected, and highlighted state. Unfortunately, we can’t add our color to the grouping of properties that change for each of the button states.


An handy generic extension in Swift:

extension UIButton {
    private func imageWithColor(color: UIColor) -> UIImage {
        let rect = CGRectMake(0.0, 0.0, 1.0, 1.0)
        UIGraphicsBeginImageContext(rect.size)
        let context = UIGraphicsGetCurrentContext()

        CGContextSetFillColorWithColor(context, color.CGColor)
        CGContextFillRect(context, rect)

        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()

        return image
    }

    func setBackgroundColor(color: UIColor, forUIControlState state: UIControlState) {
        self.setBackgroundImage(imageWithColor(color), forState: state)
    }
}

Swift 3.0

extension UIButton {
    private func imageWithColor(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(imageWithColor(color: color), for: state)
    }
}

UIButton Background Color, It seems to be possible to change the background color of the system UIButton, but it remains white in the focused state. I can switch the button to custom but I  You would need two events to be capture, UIControlEventTouchDown would be for when the user presses the button. UIControlEventTouchUpInside and UIControlEventTouchUpOutside will be for when they release the button to return it to the normal state. Return an image made from the highlight colour you want.


In Swift you can override the accessor of the highlighted (or selected) property rather than overriding the setHighlighted method

override var highlighted: Bool {
        get {
            return super.highlighted
        }
        set {
            if newValue {
                backgroundColor = UIColor.blackColor()
            }
            else {
                backgroundColor = UIColor.whiteColor()
            }
            super.highlighted = newValue
        }
    }

UIButton background color for state – iOS – Hacking with Swift forums, But you could tap into default isSelected method? I toggle it on each button press and depending on that state i change the color. Hope that  Below will be one way to go. Two IBActions. One to control background color when depressing a button, one on release. import UIKit class ViewController: UIViewController { @IBOutlet weak var button: UIButton!


UIButton+BackgroundColor on CocoaPods.org, A category or extension for UIButton to set solid background color as background image. Installation. Usage. Import <UIButton+BackgroundColor.h> , then you  How can I change UIButton title color? How to set the title of UIButton as left alignment? Is it possible to change a UIButtons background color? Creating a UIImage from a UIColor to use as a background image for UIButton ; How to change the background color of a UIButton while it's highlighted?


How To Change Background Color After Pressing Button In iPhone , In this application we will see how to change background color using button pressed. Step 2: Xcode automatically creates the directory structure and adds essential IBOutlet UIButton*thirdButton; } -(IBAction)FirstButtonPressed:(id)​sender;  With the brush mode, you can bring back textures and details from your original background. Select the tool and brush over the area where you want your original background to show. Get as creative as possible while editing your background. Change Your Background.


backgroundColor on UIButton for different touch states – Something , In iOS we can change the colour in the background in two ways. The first from UIView and Apple decided to do dot extended it for the UIButton. Tint Color You can specify a custom button tint using the tintColor property. This property sets the color of the button image and text. If you do not explicitly set a tint color, the button uses its superview’s tint color.