【发布时间】:2011-01-26 23:48:20
【问题描述】:
我是编程新手,感谢任何帮助。我试图在按下按钮后更改按钮的背景颜色。我试过 setBackgroundColor 没有成功。我不确定它是否与 UIButton 兼容。有没有办法以编程方式完成这样的任务?感谢所有想法和建议。
【问题讨论】:
标签: objective-c ipad uibutton
我是编程新手,感谢任何帮助。我试图在按下按钮后更改按钮的背景颜色。我试过 setBackgroundColor 没有成功。我不确定它是否与 UIButton 兼容。有没有办法以编程方式完成这样的任务?感谢所有想法和建议。
【问题讨论】:
标签: objective-c ipad uibutton
我建议创建一个包含您想要的背景颜色的简单图像,并通过 UIButton 中的现有方法进行设置。 (查看 Wrights Answer 的文档链接)。
UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
NSString* fileLocation = [[NSBundle mainBundle] pathForResource:@"buttonBG" ofType:@"png"];
UIImage* bgImage = [UIImage imageWithContentsOfFile:fileLocation];
if (bgImage != nil) { // check if the image was actually set
[button setBackgroundImage:bgImage forState:UIControlStateHighlighted];
} else {
NSLog(@"Error trying to read the background image");
}
这应该可以解决问题。可能有更好的方法来即时创建必要的图像,但这是我不坚定的。
[编辑:更详细的代码]
【讨论】:
buttonBG.png 的资源(包含在您的应用程序中)的路径。第二行(现在是第三行)在 bgImage 变量中创建图像。 if 语句检查是否一切正常,图像是否确实存在,然后分配它。
假设您有一个普通状态下标题为“On”的简单自定义按钮:
- (IBAction) toggleButtonState {
if ([toggleButton titleForState:UIControlStateNormal] == @"On") {
[toggleButton setTitle: @"Off" forState:UIControlStateNormal];
[toggleButton setBackgroundColor:[UIColor redColor]];
}
else {
[toggleButton setTitle: @"On" forState:UIControlStateNormal];
[toggleButton setBackgroundColor:[UIColor greenColor]];
}
}
所有其他按钮在视图前面都有一个图像,所以如果图像没有完全填满空间,你最多会看到角落发生变化。
我还建议使用图像,但出于学习目的,这会起作用。
【讨论】:
我刚刚遇到了同样的问题,最后使用 UIButton 子类来解决这个问题。我使用渐变只是因为如果你不需要它们,它看起来会更好一些,你可以简单地删除它们。我已经解释了我使用的过程,并在帖子底部包含了完整的代码。
首先为图层添加属性。我创建了两个图层,一个用于基础渐变,一个用于光泽以添加一点样式。
@interface gradientButton()
@property (nonatomic, strong) CAGradientLayer* gradientLayer;
@property (nonatomic, strong) CAGradientLayer* glossyLayer;
@end
然后在 -(void)awakeFromNib 或 -(id)initWithFrame:(CGRect)frame
中,具体取决于您是分别从情节提要还是代码加载,配置渐变并添加图层,圆角并自定义字体突出显示颜色。
-(void)awakeFromNib
{
_gradientLayer = [[CAGradientLayer alloc] init];
_gradientLayer.bounds = self.bounds;
_gradientLayer.position = CGPointMake(self.bounds.size.width/2, self.bounds.size.height/2);
[self.layer insertSublayer:_gradientLayer atIndex:0];
self.layer.cornerRadius = 5.0f;
self.layer.masksToBounds = YES;
self.layer.borderWidth = 1.0f;
_glossyLayer = [[CAGradientLayer alloc] init];
_glossyLayer.bounds = CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height/2);
_glossyLayer.position = CGPointMake(self.bounds.size.width/2, self.bounds.size.height/4);
[self.layer addSublayer:_glossyLayer];
[self setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[self setTitleColor:[UIColor yellowColor] forState:UIControlStateHighlighted];
}
接下来,覆盖 - (void)drawRect:(CGRect)rect 以应用您的图层并定义您的颜色。
#define GRADIENT_TOP [UIColor colorWithRed:38.0/255.0 green:78.0/255.0 blue:54.0/255.0 alpha:1]
#define GRADIENT_BOTTOM [UIColor colorWithRed:44.0/255.0 green:71.0/255.0 blue:56.0/255.0 alpha:1]
#define GLOSS_TOP [UIColor colorWithRed:0.70f green:0.70f blue:0.70f alpha:0.95f]
#define GLOSS_BOTTOM [UIColor colorWithRed:0.70f green:0.70f blue:0.70f alpha:0.35f]
#define GRADIENT_SELECTED_TOP [UIColor colorWithRed:138.0/255.0 green:178.0/255.0 blue:154.0/255.0 alpha:1]
#define GRADIENT_SELECTED_BOTTOM [UIColor colorWithRed:114.0/255.0 green:171.0/255.0 blue:156.0/255.0 alpha:1]
- (void)drawRect:(CGRect)rect
{
[_gradientLayer setColors:@[(id)[GRADIENT_TOP CGColor],(id)[GRADIENT_BOTTOM CGColor]]];
[_glossyLayer setColors:@[(id)[GLOSS_TOP CGColor], (id)[GLOSS_BOTTOM CGColor]]];
[super drawRect:rect];
}
最后,我们一直在等待的位,覆盖-(void)setHighlighted:(BOOL)highlighted{
,这样我们就可以应用正在寻找的高亮效果。
-(void)setHighlighted:(BOOL)highlighted{
[super setHighlighted:highlighted];
if(highlighted)
[_gradientLayer setColors:@[(id)[GRADIENT_SELECTED_TOP CGColor],(id)[GRADIENT_SELECTED_BOTTOM CGColor]]];
else
[_gradientLayer setColors:@[(id)[GRADIENT_TOP CGColor],(id)[GRADIENT_BOTTOM CGColor]]];
}
我们有了它,现在只需拖出一个 UIButton 修改类就可以了。这是完整的实现,因此您可以直接复制它。 http://pastebin.com/nUVeujyp
【讨论】:
常规 UIButton 没有 backgroundColor 选项。
我的建议是使用UISegmentedControl,它有tinColor 选项。
【讨论】:
我创建了一个子类,它获取背景颜色并为每个状态创建一个 UIImage。 对我来说,子类而不是类别更有用,所以这取决于你。
@implementation ROCRoundColorButton
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
UIColor *darkColor;
darkColor = [self darkColorFromBackgroundColor];
[self setBackgroundImage:[self imageWithColor:self.backgroundColor withSize:self.frame.size] forState:UIControlStateNormal];
[self setBackgroundImage:[self imageWithColor:darkColor withSize:self.frame.size] forState:UIControlStateSelected];
}
return self;
}
-(id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self) {
UIColor *darkColor;
darkColor = [self darkColorFromBackgroundColor];
[self setBackgroundImage:[self imageWithColor:self.backgroundColor withSize:self.frame.size] forState:UIControlStateNormal];
[self setBackgroundImage:[self imageWithColor:darkColor withSize:self.frame.size] forState:UIControlStateSelected];
}
return self;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/
#pragma mark -
#pragma mark Private methods
- (UIColor *)darkColorFromBackgroundColor
{
const float* components = CGColorGetComponents( self.backgroundColor.CGColor );
CGFloat red = components[0];
CGFloat green = components[1];
CGFloat blue = components[2];
CGFloat alpha = components[3];
if (red > 0) {
red -= 0.1;
}
if (green > 0) {
green -= 0.1;
}
if (blue > 0) {
blue -= 0.1;
}
UIColor *darkColor = [UIColor colorWithRed:red green:green blue:blue alpha:alpha];
return darkColor;
}
- (UIImage *)imageWithColor:(UIColor *)color withSize:(CGSize)size
{
UIGraphicsBeginImageContext(size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [color CGColor]);
//CGContextFillRect(context, CGRectMake(0, 0, size.width, size.height));
UIBezierPath *roundedRect = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, size.width, size.height) cornerRadius:5];
[roundedRect fillWithBlendMode: kCGBlendModeNormal alpha:1.0f];
[color setFill];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
其实你可以在storyboard中使用,在view中改变class和设置de背景色。
【讨论】: