How to change tintColor of image in UIButton with imageEdgeInsets?

倾然丶 夕夏残阳落幕 提交于 2020-01-21 19:14:24

问题


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:

I want get it:

How could I change the color of the imageButton?


回答1:


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.

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

icon.tintColor = [UIColor redColor];

Hope it will help you.




回答2:


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:


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




回答4:


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



回答5:


Swift 4 and 4.2

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

}



来源:https://stackoverflow.com/questions/41545599/how-to-change-tintcolor-of-image-in-uibutton-with-imageedgeinsets

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!