How to change tintColor of image in UIButton with imageEdgeInsets?

7.7k views Asked by At

I I have an UIButton in my objective-c application. My button was modified adding text and image like:

- (void)centerButtonAndImageWithSpacing:(CGFloat)spacing {
     CGFloat insetAmount = spacing / 2.0;
     self.imageEdgeInsets = UIEdgeInsetsMake(0, -insetAmount, 0, insetAmount);
     self.titleEdgeInsets = UIEdgeInsetsMake(0, insetAmount, 0, -insetAmount);
     self.contentEdgeInsets = UIEdgeInsetsMake(0, insetAmount, 0, insetAmount);
}

And then I add the image with this code:

[self.myButton setImage:[UIImage imageNamed:@"imageButton.png"] forState:UIControlStateNormal];

I want change the tintColor of the image, but when I try change this, my code isn't work:

[self.myButton setTintColor:[UIColor redColor]];

I'm trying use this code, but didn't work:

UIImage* img = [UIImage imageNamed:imageName];
icon.image = [img imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
icon.tintColor = [UIColor redColor];

I have this: enter image description here

I want get it: enter image description here

How could I change the color of the imageButton?

6

There are 6 answers

0
Vishnuvardhan On

We can apply the desired color for the transparent icons with the following way.

1.Drag the image on Assets.xcassets and In asset attribute inspector change the Render as attribute to Template Image.

enter image description here

2.And set the required tint color like below way.

icon.tintColor = [UIColor redColor];

Hope it will help you.

0
user3745888 On

The solution to change tintColor property was setImage with renderingMode:

 imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate

With this code:

[self.myButton setImage:[[UIImage imageNamed:@"myImageButton.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate] forState:UIControlStateNormal];

Then set image to button with this code we can set the tintColor property correctly:

 self.myButton.tintColor = [UIColor redColor];
3
Purushothaman On

Change the UIButtonType to UIButtonTypeCustom in Interface Builder or programmatically with

 UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom]; 

Change the attributes on your own and recreate the rounded corners

myButton.backgroundColor = [UIColor redColor];
myButton.layer.borderColor = [UIColor blackColor].CGColor;
myButton.layer.borderWidth = 0.5f;
myButton.layer.cornerRadius = 10.0f;

let me know the status ..

0
Syed Qamar Abbas On

Create a layer and set your desired coloured and mask in on your ImageView in your button like this.

for (UIView *view in myButton.subviews) {
        if ([view isKindOfClass:[UIImageView class]]) {
            CALayer *mask = [CALayer layer];
            mask.backgroundColor = [UIColor redColor].CGColor;
            view.layer.mask = mask;
        }
    }
0
Akbar Khan On

Swift 4 and 4.2

let img = UIImage.init(named: "buttonName")?.withRenderingMode(UIImageRenderingMode.alwaysTemplate)
            btn.setImage(img, for: .normal)
            btn.tintColor = .gray

}

enter image description here

0
kAiN On

Swift 5

let examButton = UIButton(image: UIImage(systemName: "rectangle.dock")!, tintColor: .systemRed, target: self, action: #selector(showExamController))

examButton.setTitleColor(.label, for: .normal)
examButton.setTitle("   12K", for: .normal)