UIButton的ImageView的tintColor没有改变

时间:2016-05-11 07:42:16

标签: ios objective-c uiimageview uibutton

如何更改tintColor的{​​{1}}。如果我使用button.imageView中的相同图像使用相同的渲染模式,UIImageView将会改变,但按钮的图像视图不会发生同样的情况。是因为buttonType?

tintColor

6 个答案:

答案 0 :(得分:5)

设置按钮类型“系统”,然后设置色调颜色。 这个解决方案一定会奏效。请试试这个

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

答案 1 :(得分:2)

尝试使用此代码更改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];

答案 2 :(得分:1)

你可以做点什么,

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

将色调设置为按钮,如果图像渲染模式为模板,则它也会获得此色调。

如果您想要按钮的不同色调颜色和图像不同,那么您应该使用一个临时图像视图并使用渲染模式模板将图像设置为该图像视图,然后将色调颜色设置为该图像视图,然后将该图像设置为该图像视图中的按钮。

希望这会有所帮助:)

答案 3 :(得分:1)

Xamarin iOs解决方案:

Selection

答案 4 :(得分:1)

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

使用

let button = UIButton.setImage(UIImage.imageWithRenderingModeAlwaysTemplate(named: ""), for: .normal)
button.tintColor = .red

答案 5 :(得分:0)

我找到了答案,在设置问题中提到的呈现模式后,我只需要tintColor UIButton imageView不要按下[button setTintColor:YOUR_COLOR]; 属性。

{{1}}
相关问题