我有一个UIButton它的image属性,我从界面生成器设置.我希望这张图片与superview的图片一致tintColor.使用代码,我可以将图像的渲染模式设置为UIImageRenderingModeAlwaysTemplate,但是如何从界面构建器执行此操作?
我的应用程序有一个工具栏,上面有图像按钮(UIButton的子类); 当用户打开"粗体文本"辅助功能选项时,不仅文本变为粗体,而且图像也跟随.
这是正常模式下的工具栏:

启用"粗体文本"时:

它似乎是由我的UIButton子类引起的,如下所示.我正在使用此类在单击,禁用按钮等时应用图像色调颜色,并防止必须包含每个按钮的多个状态.对于我使用的是UIImageRenderingModeAlwaysTemplate哪个据报道表现出这个观察到的行为.
我试图在界面构建器中取消选中"辅助功能"选项,但这根本没有效果.有没有办法来解决这个问题?
#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 …Run Code Online (Sandbox Code Playgroud) 我试图UIButton在用户单击时更改 的图像UIButton的颜色Interface Builder,而无需创建新图像。UIButton这些是和Normal状态的设置Highlighted:
但图像保持相同的颜色。为了使其正常工作,我缺少什么?
我已经尝试了一切,没有任何事情发生.它只是保持黑色.我错过了什么吗?我尝试使用UIButton的图像视图,但它没有做出改变.
lazy var addFriendButton: UIButton = {
self.friendButton = UIButton(type: .Custom)
self.friendButton.bounds = CGRectMake(0, 0, 120, 80)
self.friendButton.backgroundColor = UIColor.redColor()
self.friendButton.translatesAutoresizingMaskIntoConstraints = false
self.friendButton.setTitle("Add Friend", forState: .Normal)
self.friendButton.titleLabel?.font = UIFont(name: "HelveticaNeue", size: 12)
self.friendButton.addTarget(self, action: #selector(addFriend), forControlEvents: .TouchUpInside)
let addButtonImage = UIImage(named: "AddFriend")
addButtonImage?.imageWithRenderingMode(.AlwaysTemplate)
self.friendButton.setImage(addButtonImage, forState: .Normal)
self.friendButton.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
self.friendButton.imageView?.image = addButtonImage
// Not working here
self.friendButton.imageView?.tintColor = UIColor.whiteColor()
return self.friendButton
}()
Run Code Online (Sandbox Code Playgroud)