如何使用UIImageRenderingModeAlwaysTemplate防止粗体图像

我的应用程序有一个工具栏,上面有图像按钮(UIButton的子类); 当用户打开“粗体文本”辅助function选项时,不仅文本变为粗体,而且图像也跟随。

这是正常模式下的工具栏:

工具栏处于正常模式

启用“粗体文本”时:

工具栏以粗体模式显示

它似乎是由我的UIButton子类引起的,它包含在下面。 我正在使用此类在单击,禁用按钮等时应用图像色调颜色,并防止必须包含每个按钮的多个状态。 为此,我正在使用UIImageRenderingModeAlwaysTemplate , 据报道,它展示了这种观察到的行为。

我试图在界面构建器中取消选中“辅助function”选项,但这根本没有效果。 有没有办法来解决这个问题?

 #import "AppButton.h" @implementation AppButton - (id)initWithCoder:(NSCoder *)aDecoder { if (self = [super initWithCoder:aDecoder]) { [self initialize]; } return self; } - (void)initialize { self.adjustsImageWhenHighlighted = NO; [self setImage:[[self imageForState:UIControlStateNormal] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate] forState:UIControlStateNormal]; } - (void)updateButtonView { if (!self.enabled) { self.imageView.tintColor = [UIColor colorWithRGBValue:RGBValueC9]; } else if (self.highlighted) { self.imageView.tintColor = self.highlightTintColor; } else { self.imageView.tintColor = self.tintColor; } } - (void)setHighlighted:(BOOL)highlighted { [super setHighlighted:highlighted]; [self updateButtonView]; } - (void)setEnabled:(BOOL)enabled { [super setEnabled:enabled]; [self updateButtonView]; } - (void)setTintColor:(UIColor *)tintColor { [super setTintColor:tintColor]; [self updateButtonView]; } @end 

我建议您使用自定义类别为图像按钮着色。 这是一个简单的实现,它只是这样做:

的UIImage + TintImage.h

 @interface UIImage (TintImage) - (UIImage *)imageTintedWithColor:(UIColor *)tintColor; @end 

的UIImage + TintImage.m

 #import "UIImage+TintImage.h" @implementation UIImage (TintImage) - (UIImage *)imageTintedWithColor:(UIColor *)tintColor { if (tintColor == nil) { tintColor = [UIColor whiteColor]; } CGRect rect = CGRectMake(0, 0, self.size.width, self.size.height); UIGraphicsBeginImageContextWithOptions(self.size, NO, 0.0f); // Tint image [tintColor set]; UIRectFill(rect); [self drawInRect:rect blendMode:kCGBlendModeDestinationIn alpha:1.0f]; UIImage *tintedImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return tintedImage; } @end 

要使用它,只需导入"UIImage+TintImage.h" ,然后执行以下操作:

 UIImage *originalImage = [UIImage imageNamed:@"icn-menu"]; UIImage *tintedImage = [originalImage imageTintedWithColor:[UIColor blueColor]]; UIButton *homeButton = [UIButton buttonWithType:UIButtonTypeCustom]; [homeButton setImage:originalImage forState:UIControlStateNormal]; [homeButton setImage:tintedImage forState:UIControlStateHighlighted]; 

按钮展示

感谢Rufel的回答,我能够解决我的问题并同时减少我class级的代码:

 #import "AppButton.h" @interface AppButton () @property (readonly) UIImage *normalImage; @end @implementation AppButton @synthesize highlightTintColor = _highlightTintColor; - (id)initWithCoder:(NSCoder *)aDecoder { if (self = [super initWithCoder:aDecoder]) { [self initialize]; } return self; } - (UIImage *)normalImage { return [self imageForState:UIControlStateNormal]; } - (void)initialize { self.adjustsImageWhenHighlighted = NO; // set disabled image [self setImage:[self image:self.normalImage tintedWithColor:[UIColor colorWithRGBValue:RGBValueC9]] forState:UIControlStateDisabled]; } - (void)setHighlightTintColor:(UIColor *)highlightTintColor { _highlightTintColor = highlightTintColor; // update highlighted image if (highlightTintColor) { [self setImage:[self image:self.normalImage tintedWithColor:highlightTintColor] forState:UIControlStateHighlighted]; } } - (UIImage *)image:(UIImage *)image tintedWithColor:(UIColor *)tintColor { CGRect rect = CGRectMake(0, 0, image.size.width, image.size.height); UIGraphicsBeginImageContextWithOptions(image.size, NO, 0.0f); // Tint image [tintColor set]; UIRectFill(rect); [image drawInRect:rect blendMode:kCGBlendModeDestinationIn alpha:1.0f]; UIImage *tintedImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return tintedImage; } @end 

这是rufel的快速3版本答案,

 extension UIImageView { fileprivate func tintImage(color: UIColor){ guard let _image = image else { return } let rect = CGRect(x: 0.0, y: 0.0, width: _image.size.width , height: _image.size.height ) UIGraphicsBeginImageContextWithOptions(_image.size , false, _image.scale) color.set() UIRectFill(rect) _image.draw(in: rect, blendMode: CGBlendMode.destinationIn, alpha: 1.0) image = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() } } 

要么

 extension UIImage { static func imageTinted(image: UIImage?, color: UIColor) -> UIImage? { let rect = CGRect(x: 0.0, y: 0.0, width: image?.size.width ?? 0.0, height: image?.size.height ?? 0.0) UIGraphicsBeginImageContextWithOptions(image?.size ?? CGSize.zero, false, image?.scale ?? 2.0) color.set() UIRectFill(rect) image?.draw(in: rect, blendMode: CGBlendMode.destinationIn, alpha: 1.0) let tinted = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext(); return tinted; } }