Color Tint Uibutton Image

Color Tint UIButton Image

As of iOS 7, there is a new method on UIImage to specify the rendering mode. Using the rendering mode UIImageRenderingModeAlwaysTemplate will allow the image color to be controlled by the button's tint color.

Objective-C

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *image = [[UIImage imageNamed:@"image_name"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
[button setImage:image forState:UIControlStateNormal];
button.tintColor = [UIColor redColor];

Swift

let button = UIButton(type: .custom)
let image = UIImage(named: "image_name")?.withRenderingMode(.alwaysTemplate)
button.setImage(image, for: .normal)
button.tintColor = UIColor.red

How to set the UIButton tint color of a system image when pressed?

You have to also disable adjusting image property:

buttonLogin?.adjustsImageWhenHighlighted = false

Now grey effect should be vanished

tintColor of UIButton's ImageView is not changing

Set Button type "System" and then set tint color.
This solution will definitely work. Please try this

UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
[btn setTintColor:YOUR_COLOR];
[btn setImage:YOUR_Image];

Change tint image color when pressing uibutton in Swift

I don't know if is better approach or answer, but, i maybe could delivery this using this approach:

Create a method that will "fill" the color for your choice button and "clear" color to others , but its a method that loop through UIScrollView and look for each UIButton. Something like this :

func setBackgroundColorButton(color:UIColor , buttonTag:Int){
for view in self.scrollView.subviews as [UIView] {
if let btn = view as? UIButton {
if btn == buttonTag {
btn.tintColor = color
} else {
btn.tintColor = UIColor.whiteColor()
}
}
}
}

This is the concept, i didn't tested, but maybe just need adjust to search inside your scroll view or similar.

But with this will be work nice i believe :D

Change ImageView's tint color in UIButton based on state

check the below code:-

@IBAction func onClickButton(_ sender: UIButton) {
sender.imageView.image = sender.imageView.image?.withRenderingMode(.alwaysTemplate)
if sender.isSelected || sender.isHighlighted{
sender.imageView.tintColor = .red
}else{
sender.imageView.tintColor = .blue
}
}

UIButton image tint not working

Try setting the image to render as a template image. You can do this in your .xcasset folder selecting the image set, opening the attributes inspector and setting render as to "template image."

Sample Image

Changing color of image embedded to a button (Swift3)

Here's a simpler way to do this without any extension and without resetting the color on touch:

let stencil = myImage.withRenderingMode(.alwaysTemplate) // use your UIImage here
myButton.setImage(stencil, for: .normal) // assign it to your UIButton
myButton.tintColor = UIColor.white // set a color


Related Topics



Leave a reply



Submit