IOS 7按钮图像颜色
Posted
技术标签:
【中文标题】IOS 7按钮图像颜色【英文标题】:IOS 7 button image color 【发布时间】:2014-03-27 10:30:42 【问题描述】:我的UIButton
的一部分是UIImage
,ios 7 将其颜色从黑色变为蓝色。我尝试了这里提供的解决方案:将按钮类型更改为UIButtonTypeSystem
,但这无济于事,图像仍然是蓝色的。这是我正在使用的通用代码:
UIButton *iWantPlan = [UIButton buttonWithType:UIButtonTypeSystem];
iWantPlan.frame = CGRectMake(self.view.bounds.size.width/2-bgImageWidth/2, planBg.frame.origin.y + planBg.frame.size.height + 20, bgImageWidth, bgImageWidth/4+5);
iWantPlan.titleLabel.font = [UIFont systemFontOfSize:20];
[iWantPlan addTarget:self action:@selector(goToPersonalDetails) forControlEvents:UIControlEventTouchDown];
[self.view addSubview:iWantPlan];
iWantPlan.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
iWantPlan.imageView.frame = CGRectMake(bgImageWidth - 20, 10, 25, 25);
UIImage *viImage = [UIImage imageNamed:@"Icon_V.png"];
UIImage *viImageTap = [UIImage imageNamed:@"Icon_V_Tap.png"];
[iWantPlan setImage:viImage forState:UIControlStateNormal];
[iWantPlan setImage:viImageTap forState:UIControlStateSelected];
有解决办法吗? (顺便说一句,这真的很令人沮丧:)
【问题讨论】:
【参考方案1】:使用:imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal
确定你没有蓝色背景:
UIImage *viImage = [[UIImage imageNamed:@"Icon_V.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
UIImage *viImageTap = [[UIImage imageNamed:@"Icon_V_Tap.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
来自Apple Documentation UIImageRenderingMode:
始终绘制原始图像,而不将其视为模板:UIImageRenderingModeAlwaysOriginal
另外,这可以帮助你:
UIButton *iWantPlan = [UIButton buttonWithType:UIButtonTypeCustom];
...
[iWantPlan setImage:viImageTap forState:UIControlStateHighlighted];
【讨论】:
【参考方案2】:只需将按钮的类型 UIButtonTypeSystem
更改为 UIButtonTypeCustom
【讨论】:
以上是关于IOS 7按钮图像颜色的主要内容,如果未能解决你的问题,请参考以下文章
iOS:如果背景图像与按钮颜色相同,则更改我的 UIButton 的颜色
更改 MPVolumeView 路由按钮 iOS 7 的颜色