ios 在 UIImageView 上使用 Tint 颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22170688/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 23:38:21  来源:igfitidea点击:

Using Tint color on UIImageView

iosxcodeuiimageviewuibuttontintcolor

提问by Marko Zadravec

I have my own subclass of UIButton. I add UIImageViewon it and add an image. I would like to paint it over the image with a tint color but it doesn't work.

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

So far I have:

到目前为止,我有:

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

and on selection :

和选择:

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

What did I do wrong? (The color of the image always stays the same as it was originally.)

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

回答by Marko Zadravec

Instead of this code:

而不是这个代码:

[image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];

you should have:

你应该有:

image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];

Use this in Swift 4.1

Swift 4.1 中使用它

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

回答by StackUnderflow

You can also just set this on your asset. Make sure your image contains all white pixels + transparent. enter image description here

您也可以在您的资产上设置它。确保您的图像包含所有白色像素 + 透明。 在此处输入图片说明

回答by odemolliens

(Can't edit @Zhaolong Zhong post)

(无法编辑@Zhaolong Zhong 帖子)

In swift 3.0, you can do:

在 swift 3.0 中,您可以执行以下操作:

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

yourImageView.image = image

yourImageView.tintColor = UIColor.blue

回答by Zhaolong Zhong

In swift 2.0+, you can do:

在 swift 2.0+ 中,您可以执行以下操作:

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

yourImageView.image = image

yourImageView.tintColor = UIColor.blueColor()

回答by Nischal Hada

Objective C

目标 C

self.imgView.image = [self.imgView.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
[self.imgView setTintColor:[UIColor darkGrayColor]];

回答by Daniel

One step further. This is a drop-in subclass of UIImageView. (Not exact solution for original question.) Use in Interface Builder by setting class name to TintedImageView. Updates in real-time inside the designer as tint color changes.

更进一步。这是 UIImageView 的一个嵌入式子类。(不是原始问题的确切解决方案。)通过将类名设置为 TintedImageView 在 Interface Builder 中使用。随着色调颜色的变化,在设计器内部实时更新。

(Swift 3.1, Xcode 8.3)

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

回答by Gary Davies

Make the imageView

制作图像视图

    let imageView = UIImageView(frame: frame!)
    imageView.contentMode = .ScaleAspectFit
    imageView.tintColor = tintColor

Make the image

制作图像

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

Wire them together

将它们连接在一起

    imageView?.image = image

Display it

显示它

view.addSubview(imageView)

回答by ingconti

all said is correct. my contribution If You cannot / dont want to apply to every UiImageView, OR for efficiency You need to render ONCE (or example for cells of tableviews)

说的都是对的。我的贡献如果你不能/不想应用到每个 UiImageView,或者为了效率你需要渲染一次(或者例如 tableviews 的单元格)

func tint(with color: UIColor) -> UIImage {
        var image = withRenderingMode(.alwaysTemplate)
        UIGraphicsBeginImageContextWithOptions(size, false, scale)
        color.set()

        image.draw(in: CGRect(origin: .zero, size: size))
        image = UIGraphicsGetImageFromCurrentImageContext()!
        UIGraphicsEndImageContext()
        return image
    }

And set to all UI elements this UIImage.

并将此 UIImage 设置为所有 UI 元素。

回答by rockdaswift

@odemolliens answer should just work.

@odemolliens 的答案应该可以正常工作。

But if you are still having issues, make sure that the tint color you are applying to the UIImageView is different from the one defined in the Interface Builder.

但是,如果您仍然遇到问题,请确保您应用到 UIImageView 的色调颜色与 Interface Builder 中定义的颜色不同。