在UIImageView上使用Tint颜色

Mar*_*vec 105 xcode uibutton uiimageview tintcolor ios

我有自己的子类UIButton.我添加UIImageView它并添加图像.我想用浅色涂在图像上,但它不起作用.

到目前为止,我有:

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {

        self.backgroundColor = [UIColor clearColor];
        self.clipsToBounds = YES;

        self.circleView = [[UIView alloc]init];
        self.circleView.backgroundColor = [UIColor whiteColor];
        self.circleView.layer.borderColor = [[Color getGraySeparatorColor]CGColor];
        self.circleView.layer.borderWidth = 1;
        self.circleView.userInteractionEnabled = NO;
        self.circleView.translatesAutoresizingMaskIntoConstraints = NO;
        [self addSubview:self.circleView];

        self.iconView = [[UIImageView alloc]init];
        [self.iconView setContentMode:UIViewContentModeScaleAspectFit];
        UIImage * image = [UIImage imageNamed:@"more"];
        [image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
        self.iconView.image = image;
        self.iconView.translatesAutoresizingMaskIntoConstraints = NO;
        [self.circleView addSubview:self.iconView];
        ...
Run Code Online (Sandbox Code Playgroud)

并在选择:

- (void) setSelected:(BOOL)selected
{
    if (selected) {
        [self.iconView setTintColor:[UIColor redColor]];
        [self.circleView setTintColor:[UIColor redColor]];
    }
    else{
        [self.iconView setTintColor:[UIColor blueColor]];
        [self.circleView setTintColor:[UIColor blueColor]];
    }  
}
Run Code Online (Sandbox Code Playgroud)

我做错了什么?(图像的颜色始终保持原样.)

Mar*_*vec 218

而不是这段代码:

[image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
Run Code Online (Sandbox Code Playgroud)

你应该有:

image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
Run Code Online (Sandbox Code Playgroud)

Swift 4.1中使用它

image = UIImage(named: "name")!.withRenderingMode(.alwaysTemplate)
Run Code Online (Sandbox Code Playgroud)

  • 您可以在资源中选择图像,然后在右侧面板中选择默认渲染模式 (44认同)
  • 这不是一个愚蠢的错误,而是一个非常重要的细节.感谢发布此内容.你刚给我留了很多时间.(更正错字) (8认同)

Sta*_*low 75

您也可以在资产上设置此项.确保您的图片包含所有白色像素+透明. 在此输入图像描述

  • @Banana,这是未使用色调颜色的图像的已知问题.我曾经向Apple报告过它,并将它标记为副本,所以他们肯定知道它.解决方法是不将UIImageView设置为Storyboard上的图像.所以只需要一个空白的UIImageView,然后在viewDidLoad(或其他地方)中设置它,然后你会在图像上看到tintColor. (10认同)
  • 我做了所有这些,但由于某种原因,tintColor对我不起作用.知道我还能尝试什么吗? (6认同)
  • 奇怪的是,在我的第一次运行中,只有2/3的图像拾取了色调.在清理并构建所有图像后,拾取色调. (3认同)

ode*_*ens 35

(不能编辑@Zhaolong Zhong帖子)

在swift 3.0中,您可以:

let image = UIImage(named: "your_image_name")!.withRenderingMode(.alwaysTemplate)

yourImageView.image = image

yourImageView.tintColor = UIColor.blue
Run Code Online (Sandbox Code Playgroud)


小智 19

在swift 2.0+中,您可以:

let image = UIImage(named: "your_image_name")!.imageWithRenderingMode(.AlwaysTemplate)

yourImageView.image = image

yourImageView.tintColor = UIColor.blueColor()
Run Code Online (Sandbox Code Playgroud)


Nis*_*ada 9

目标C.

self.imgView.image = [self.imgView.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
[self.imgView setTintColor:[UIColor darkGrayColor]];
Run Code Online (Sandbox Code Playgroud)


Dan*_*iel 6

更进一步.这是UIImageView的一个子类.(不是原始问题的精确解决方案.)通过将类名设置为TintedImageView在Interface Builder中使用.设计师在色彩变化时实时更新.

(Swift 3.1,Xcode 8.3)

import UIKit

@IBDesignable class TintedImageView: UIImageView {
    override func prepareForInterfaceBuilder() {
        self.configure()
    }

    override func awakeFromNib() {
        super.awakeFromNib()

        self.configure()
    }

    @IBInspectable override var tintColor: UIColor! {
        didSet {
            self.configure()
        }
    }

    private func configure() {
        self.image = self.image?.withRenderingMode(UIImageRenderingMode.alwaysTemplate)
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 不工作,但它修改func配置为:self.image = self.image?.withRenderingMode(UIImageRenderingMode.alwaysTemplate)let color = super.tintColor super.tintColor = UIColor.clear super.tintColor = color (2认同)

Gar*_*ies 5

制作imageView

    let imageView = UIImageView(frame: frame!)
    imageView.contentMode = .ScaleAspectFit
    imageView.tintColor = tintColor
Run Code Online (Sandbox Code Playgroud)

制作图像

    let mainBundle = NSBundle.mainBundle()
    var image = UIImage(named: filename!, inBundle: mainBundle, compatibleWithTraitCollection: nil)
    image = image?.imageWithRenderingMode(.AlwaysTemplate)
Run Code Online (Sandbox Code Playgroud)

将它们连接在一起

    imageView?.image = image
Run Code Online (Sandbox Code Playgroud)

显示它

view.addSubview(imageView)
Run Code Online (Sandbox Code Playgroud)