user3745888
user3745888

Reputation: 6253

How to change tintColor of image in UIButton with imageEdgeInsets?

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?

Upvotes: 2

Views: 7971

Answers (6)

kAiN
kAiN

Reputation: 2793

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)

Upvotes: 0

Akbar Khan
Akbar Khan

Reputation: 2417

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

Upvotes: 2

Vishnuvardhan
Vishnuvardhan

Reputation: 5107

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.

Upvotes: 3

user3745888
user3745888

Reputation: 6253

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];

Upvotes: 3

Syed Qamar Abbas
Syed Qamar Abbas

Reputation: 3677

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;
        }
    }

Upvotes: 1

Purushothaman
Purushothaman

Reputation: 358

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 ..

Upvotes: 1

Related Questions