在我的应用程序的某些时候,我有一个突出显示的UIButton(例如当用户有他的手指在按钮上),我需要改变背景颜色,而按钮是突出显示的(所以当用户的手指仍然在按钮上)。
我尝试了以下方法:
_button.backgroundColor = [UIColor redColor];
但这并不奏效。颜色保持不变。当按钮没有高亮显示时,我尝试了同一段代码,它工作得很好。我还尝试在改变颜色后调用-setNeedsDisplay,它没有任何效果。
如何强制按钮改变背景颜色?
在我的应用程序的某些时候,我有一个突出显示的UIButton(例如当用户有他的手指在按钮上),我需要改变背景颜色,而按钮是突出显示的(所以当用户的手指仍然在按钮上)。
我尝试了以下方法:
_button.backgroundColor = [UIColor redColor];
但这并不奏效。颜色保持不变。当按钮没有高亮显示时,我尝试了同一段代码,它工作得很好。我还尝试在改变颜色后调用-setNeedsDisplay,它没有任何效果。
如何强制按钮改变背景颜色?
当前回答
在Swift中,你可以重写高亮显示(或选中)属性的访问器,而不是重写setHighlighted方法
override var highlighted: Bool {
get {
return super.highlighted
}
set {
if newValue {
backgroundColor = UIColor.blackColor()
}
else {
backgroundColor = UIColor.whiteColor()
}
super.highlighted = newValue
}
}
其他回答
为了解决这个问题,我创建了一个Category来处理UIButtons的backgroundColor状态: ButtonBackgroundColor-iOS
您可以将类别安装为pod。
易于与Objective-C一起使用
@property (nonatomic, strong) UIButton *myButton;
...
[self.myButton bbc_backgroundColorNormal:[UIColor redColor]
backgroundColorSelected:[UIColor blueColor]];
甚至更容易使用Swift:
import ButtonBackgroundColor
...
let myButton:UIButton = UIButton(type:.Custom)
myButton.bbc_backgroundColorNormal(UIColor.redColor(), backgroundColorSelected: UIColor.blueColor())
我建议您导入豆荚:
platform :ios, '8.0'
use_frameworks!
pod 'ButtonBackgroundColor', '~> 1.0'
使用use_frameworks !在你的Podfile中使你更容易使用Swift和objective-C。
重要的
我还写了一篇博客来提供更多的信息。
更新:
使用UIButtonBackgroundColor Swift库。
OLD:
使用下面的帮助工具创建一个1px x 1px的图像,灰度填充颜色:
UIImage *image = ACUTilingImageGray(248/255.0, 1);
或RGB填充颜色:
UIImage *image = ACUTilingImageRGB(253/255.0, 123/255.0, 43/255.0, 1);
然后,使用该图像设置按钮的背景图像:
[button setBackgroundImage:image forState:UIControlStateNormal];
助手
#pragma mark - Helpers
UIImage *ACUTilingImageGray(CGFloat gray, CGFloat alpha)
{
return ACUTilingImage(alpha, ^(CGContextRef context) {
CGContextSetGrayFillColor(context, gray, alpha);
});
}
UIImage *ACUTilingImageRGB(CGFloat red, CGFloat green, CGFloat blue, CGFloat alpha)
{
return ACUTilingImage(alpha, ^(CGContextRef context) {
CGContextSetRGBFillColor(context, red, green, blue, alpha);
});
}
UIImage *ACUTilingImage(CGFloat alpha, void (^setFillColor)(CGContextRef context))
{
CGRect rect = CGRectMake(0, 0, 0.5, 0.5);
UIGraphicsBeginImageContextWithOptions(rect.size, alpha == 1, 0);
CGContextRef context = UIGraphicsGetCurrentContext();
setFillColor(context);
CGContextFillRect(context, rect);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
注意:ACU是我的Cocoa Touch静态库称为Acani Utilities的类前缀,其中AC是Acani, U是Utilities。
你可以子类化UIButton并创建forState。
colourButton.h
#import <UIKit/UIKit.h>
@interface colourButton : UIButton
-(void)setBackgroundColor:(UIColor *)backgroundColor forState:(UIControlState)state;
@end
颜色按钮.m
#import "colourButton.h"
@implementation colourButton
{
NSMutableDictionary *colours;
}
-(id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
// If colours does not exist
if(!colours)
{
colours = [NSMutableDictionary new]; // The dictionary is used to store the colour, the key is a text version of the ENUM
colours[[NSString stringWithFormat:@"%lu", UIControlStateNormal]] = (UIColor*)self.backgroundColor; // Store the original background colour
}
return self;
}
-(void)setBackgroundColor:(UIColor *)backgroundColor forState:(UIControlState)state
{
// If it is normal then set the standard background here
if(state & UIControlStateNormal)
{
[super setBackgroundColor:backgroundColor];
}
// Store the background colour for that state
colours[[NSString stringWithFormat:@"%lu", state]]= backgroundColor;
}
-(void)setHighlighted:(BOOL)highlighted
{
// Do original Highlight
[super setHighlighted:highlighted];
// Highlight with new colour OR replace with orignial
if (highlighted && colours[[NSString stringWithFormat:@"%lu", UIControlStateHighlighted]])
{
self.backgroundColor = colours[[NSString stringWithFormat:@"%lu", UIControlStateHighlighted]];
}
else
{
self.backgroundColor = colours[[NSString stringWithFormat:@"%lu", UIControlStateNormal]];
}
}
-(void)setSelected:(BOOL)selected
{
// Do original Selected
[super setSelected:selected];
// Select with new colour OR replace with orignial
if (selected && colours[[NSString stringWithFormat:@"%lu", UIControlStateSelected]])
{
self.backgroundColor = colours[[NSString stringWithFormat:@"%lu", UIControlStateSelected]];
}
else
{
self.backgroundColor = colours[[NSString stringWithFormat:@"%lu", UIControlStateNormal]];
}
}
@end
注释(这是一个例子,我知道有问题,这里有一些)
我已经使用了一个NSMutableDictionay来存储每个状态的UIColor,我必须为键做一个讨厌的文本转换,因为UIControlState不是一个很好的直Int。如果它在哪里,你可以init一个数组与许多对象,并使用状态作为一个索引。
因此,你有很多困难,例如一个选中&禁用按钮,一些更多的逻辑是需要的。
另一个问题是,如果你试图同时设置多种颜色,我没有尝试过一个按钮,但如果你能这样做,它可能不会工作
[btn setBackgroundColor:colour forState:UIControlStateSelected & UIControlStateHighlighted];
我假设这是StoryBoard,没有init和initWithFrame如果你需要,可以添加它们。
extension UIButton {
func setBackgroundColor(color: UIColor, forState: UIControl.State) {
let size = CGSize(width: 1, height: 1)
UIGraphicsBeginImageContext(size)
let context = UIGraphicsGetCurrentContext()
context?.setFillColor(color.cgColor)
context?.fill(CGRect(origin: CGPoint.zero, size: size))
let colorImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
setBackgroundImage(colorImage, for: forState)
}
}
谢谢@Maverick
你可以重写UIButton的setHighlighted方法。
objective - c
- (void)setHighlighted:(BOOL)highlighted {
[super setHighlighted:highlighted];
if (highlighted) {
self.backgroundColor = UIColorFromRGB(0x387038);
} else {
self.backgroundColor = UIColorFromRGB(0x5bb75b);
}
}
Swift 3.0和Swift 4.1
override open var isHighlighted: Bool {
didSet {
backgroundColor = isHighlighted ? UIColor.black : UIColor.white
}
}