3

How to change tintColor of button.imageView. If I use the same image in UIImageView with the same rendering mode as below the tintColor will change but the same is not happening for button's image view. Is it because of buttonType ?

    UIButton *frontButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [frontButton setImage:[[UIImage imageNamed:FRONT_IMAGE] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate] forState:UIControlStateNormal];
    [frontButton.imageView setTintColor:[UIColor redColor]];
    [self addSubview:frontButton];
Sunil Rao
  • 800
  • 2
  • 6
  • 23
  • 1
    [link](http://stackoverflow.com/questions/14511639/how-to-tint-the-background-images-of-an-uibutton-programmatically-and-dynamicall) hope this link help you. – Mitesh Dobareeya May 11 '16 at 08:00

6 Answers6

11

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];
Monika Patel
  • 2,287
  • 3
  • 20
  • 45
2

Try This code to change the tint color inside Button :

UIButton *frontButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
UIImageView *imageV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:FRONT_IMAGE]];
imageV.image = [imageV.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
imageV.tintColor = [UIColor redColor];
[frontButton setImage:imageV.image forState:UIControlStateNormal];
Kirit Modi
  • 23,155
  • 15
  • 89
  • 112
2

Swift 4.0

extension UIImage {
    public static func imageWithRenderingModeAlwaysTemplate(named: String) -> UIImage? {
        let image = UIImage(named: named)?.withRenderingMode(.alwaysTemplate)
        let imageView = UIImageView(image: image)
        return imageView.image
    }
}

Using

let button = UIButton.setImage(UIImage.imageWithRenderingModeAlwaysTemplate(named: ""), for: .normal)
button.tintColor = .red
Giang
  • 2,384
  • 2
  • 25
  • 26
1

you can do something like,

UIButton *frontButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[frontButton setImage:[[UIImage imageNamed:FRONT_IMAGE] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate] forState:UIControlStateNormal];
[frontButton setTintColor:[UIColor redColor]];
[self addSubview:frontButton];

Set tint color to button and if image rendering mode is template then it will also get this tint color.

If you want different tint color for button and different for image then you should take one temporary imageview and set image to that imageview with rendering mode template and then set tint color to that imageview and then set image to button from that imageview.

Hope this will help :)

Ketan Parmar
  • 27,092
  • 9
  • 50
  • 75
0

I found the answer, I just need to give tintColor to UIButton not to button's imageView property after setting the rendering mode as mentioned in the question.

[button setTintColor:YOUR_COLOR];
Sunil Rao
  • 800
  • 2
  • 6
  • 23
0

Xamarin iOs Solution:

  var image = uiButton.ImageView.Image.ImageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate);
  uiButton.SetImage(image, UIControlState.Normal);
  uiButton.TintColor = effect.TintColor.ToUIColor();
Rudy Spano
  • 1,379
  • 10
  • 13