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)
Sta*_*low 75
您也可以在资产上设置此项.确保您的图片包含所有白色像素+透明.

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)
目标C.
self.imgView.image = [self.imgView.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
[self.imgView setTintColor:[UIColor darkGrayColor]];
Run Code Online (Sandbox Code Playgroud)
更进一步.这是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)
制作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)
| 归档时间: |
|
| 查看次数: |
68893 次 |
| 最近记录: |