Ios 如何按状态设置UIButton背景色

Ios 如何按状态设置UIButton背景色,ios,uibutton,Ios,Uibutton,如我们所知,UIButton基于UIControlState提供图像设定器。但它似乎不提供基于uicontrol状态设置背景色 如何操作?从UIColor获取UIImage,将该图像用作按钮的背景图像 - (UIImage *)imageWithColor:(UIColor *)color { CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f); UIGraphicsBeginImageContext(rect.size);

如我们所知,
UIButton
基于
UIControlState
提供图像设定器。但它似乎不提供基于
uicontrol状态设置背景色

如何操作?

UIColor
获取
UIImage
,将该图像用作按钮的背景图像

- (UIImage *)imageWithColor:(UIColor *)color
{
    CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return image;
}

[button setBackgroundImage:[self imageWithColor:[UIColor lightGrayColor]] forState:UIControlStateNormal];
[button setBackgroundImage:[self imageWithColor:[UIColor redColor]] forState:UIControlStateHighlighted];

UIColor
获取
UIImage
,将该图像用作按钮的背景图像

- (UIImage *)imageWithColor:(UIColor *)color
{
    CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return image;
}

[button setBackgroundImage:[self imageWithColor:[UIColor lightGrayColor]] forState:UIControlStateNormal];
[button setBackgroundImage:[self imageWithColor:[UIColor redColor]] forState:UIControlStateHighlighted];

是的,没错。。。您需要在
UIButton
子类中处理此问题

这是我使用的
StyleKitButton
UIButton
子类。它将每个模式的背景色存储在字典中,然后对触摸进行一些奇特的处理以正确过渡

用法

StyleKitButton *button = [[StyleKitButton alloc] init];
button.layer.borderColor = [UIColor blue].CGColor;

[button setTitleColor:[UIColor white] forState:UIControlStateNormal];
[button setBackgroundColor:[UIColor blue] forState:UIControlStateNormal];

[button setTitleColor:[UIColor white] forState:UIControlStateHighlighted];
[button setBackgroundColor:[UIColor purple] forState:UIControlStateHighlighted];
类别:

#import <UIKit/UIKit.h>

@interface StyleKitButton : UIButton

@end

//------------------------------------------------------


#import "StyleKitButton.h"

@interface StyleKitButton()

@property (nonatomic, strong) NSMutableDictionary *backgroundColors;

@end

@implementation StyleKitButton

#pragma mark - Background Colors

- (void)setBackgroundColor:(UIColor *)backgroundColor forState:(UIControlState)state {
    if (!self.backgroundColors) {
        self.backgroundColors = [[NSMutableDictionary alloc] init];
    }

    if (backgroundColor) {
        self.backgroundColors[@(state)] = backgroundColor;
    }

    if (state == UIControlStateNormal) {
        self.backgroundColor = backgroundColor;
    }
}

- (void)transitionBackgroundToColor:(UIColor*)color {
    CATransition *animation = [CATransition animation];
    animation.type = kCATransitionFade;
    [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
    [self.layer addAnimation:animation forKey:@"EaseOut"];
    self.backgroundColor = color;
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [super touchesBegan:touches withEvent:event];

    UIColor *selectedColor = self.backgroundColors[@(UIControlStateHighlighted)];
    if (selectedColor) {
        [self transitionBackgroundToColor:selectedColor];
    }
}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
    [super touchesCancelled:touches withEvent:event];

    UIColor *normalColor = self.backgroundColors[@(UIControlStateNormal)];
    if (normalColor) {
        [self transitionBackgroundToColor:normalColor];
    }
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    [super touchesEnded:touches withEvent:event];

    UIColor *normalColor = self.backgroundColors[@(UIControlStateNormal)];
    if (normalColor) {
        [self transitionBackgroundToColor:normalColor];
    }
}

@end
#导入
@接口样式KitButton:UIButton
@结束
//------------------------------------------------------
#导入“StyleKitButton.h”
@接口StyleKitButton()
@属性(非原子,强)NSMutableDictionary*背景色;
@结束
@实现样式按钮
#pragma标记-背景色
-(void)setBackgroundColor:(UIColor*)backgroundColor for状态:(UIControlState)状态{
如果(!self.backgroundColors){
self.backgroundColors=[[NSMutableDictionary alloc]init];
}
if(背景色){
自身背景色[@(状态)]=背景色;
}
如果(状态==uicontrol状态正常){
self.backgroundColor=背景色;
}
}
-(无效)转换背景颜色:(UIColor*)颜色{
CATTransition*动画=[CATTransition动画];
animation.type=kCATransitionFade;
[animation SetTiming Function:[CamediaTiming Function,名称:KCAMediaTiming Function easeineAseOut];
[self.layer addAnimation:animation forKey:@“EaseOut”];
self.backgroundColor=颜色;
}
-(无效)触摸开始:(NSSet*)触摸事件:(UIEvent*)事件{
[超级触摸开始:触摸事件:事件];
UIColor*selectedColor=self.BackgroundColor[@(UIControlStateHighlighted)];
如果(已选择颜色){
[自转换背景颜色:selectedColor];
}
}
-(无效)触控取消:(NSSet*)触控事件:(UIEvent*)事件{
[超级触控取消:触控事件:事件];
UIColor*normalColor=self.BackgroundColor[@(UIControlStateNormal)];
if(正常颜色){
[自转换背景颜色:正常颜色];
}
}
-(void)touchesend:(NSSet*)toucheevent:(UIEvent*)event{
[超级触控:触控事件:事件];
UIColor*normalColor=self.BackgroundColor[@(UIControlStateNormal)];
if(正常颜色){
[自转换背景颜色:正常颜色];
}
}
@结束

是的,没错。。。您需要在
UIButton
子类中处理此问题

这是我使用的
StyleKitButton
UIButton
子类。它将每个模式的背景色存储在字典中,然后对触摸进行一些奇特的处理以正确过渡

用法

StyleKitButton *button = [[StyleKitButton alloc] init];
button.layer.borderColor = [UIColor blue].CGColor;

[button setTitleColor:[UIColor white] forState:UIControlStateNormal];
[button setBackgroundColor:[UIColor blue] forState:UIControlStateNormal];

[button setTitleColor:[UIColor white] forState:UIControlStateHighlighted];
[button setBackgroundColor:[UIColor purple] forState:UIControlStateHighlighted];
类别:

#import <UIKit/UIKit.h>

@interface StyleKitButton : UIButton

@end

//------------------------------------------------------


#import "StyleKitButton.h"

@interface StyleKitButton()

@property (nonatomic, strong) NSMutableDictionary *backgroundColors;

@end

@implementation StyleKitButton

#pragma mark - Background Colors

- (void)setBackgroundColor:(UIColor *)backgroundColor forState:(UIControlState)state {
    if (!self.backgroundColors) {
        self.backgroundColors = [[NSMutableDictionary alloc] init];
    }

    if (backgroundColor) {
        self.backgroundColors[@(state)] = backgroundColor;
    }

    if (state == UIControlStateNormal) {
        self.backgroundColor = backgroundColor;
    }
}

- (void)transitionBackgroundToColor:(UIColor*)color {
    CATransition *animation = [CATransition animation];
    animation.type = kCATransitionFade;
    [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
    [self.layer addAnimation:animation forKey:@"EaseOut"];
    self.backgroundColor = color;
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [super touchesBegan:touches withEvent:event];

    UIColor *selectedColor = self.backgroundColors[@(UIControlStateHighlighted)];
    if (selectedColor) {
        [self transitionBackgroundToColor:selectedColor];
    }
}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
    [super touchesCancelled:touches withEvent:event];

    UIColor *normalColor = self.backgroundColors[@(UIControlStateNormal)];
    if (normalColor) {
        [self transitionBackgroundToColor:normalColor];
    }
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    [super touchesEnded:touches withEvent:event];

    UIColor *normalColor = self.backgroundColors[@(UIControlStateNormal)];
    if (normalColor) {
        [self transitionBackgroundToColor:normalColor];
    }
}

@end
#导入
@接口样式KitButton:UIButton
@结束
//------------------------------------------------------
#导入“StyleKitButton.h”
@接口StyleKitButton()
@属性(非原子,强)NSMutableDictionary*背景色;
@结束
@实现样式按钮
#pragma标记-背景色
-(void)setBackgroundColor:(UIColor*)backgroundColor for状态:(UIControlState)状态{
如果(!self.backgroundColors){
self.backgroundColors=[[NSMutableDictionary alloc]init];
}
if(背景色){
自身背景色[@(状态)]=背景色;
}
如果(状态==uicontrol状态正常){
self.backgroundColor=背景色;
}
}
-(无效)转换背景颜色:(UIColor*)颜色{
CATTransition*动画=[CATTransition动画];
animation.type=kCATransitionFade;
[animation SetTiming Function:[CamediaTiming Function,名称:KCAMediaTiming Function easeineAseOut];
[self.layer addAnimation:animation forKey:@“EaseOut”];
self.backgroundColor=颜色;
}
-(无效)触摸开始:(NSSet*)触摸事件:(UIEvent*)事件{
[超级触摸开始:触摸事件:事件];
UIColor*selectedColor=self.BackgroundColor[@(UIControlStateHighlighted)];
如果(已选择颜色){
[自转换背景颜色:selectedColor];
}
}
-(无效)触控取消:(NSSet*)触控事件:(UIEvent*)事件{
[超级触控取消:触控事件:事件];
UIColor*normalColor=self.BackgroundColor[@(UIControlStateNormal)];
if(正常颜色){
[自转换背景颜色:正常颜色];
}
}
-(void)touchesend:(NSSet*)toucheevent:(UIEvent*)event{
[超级触控:触控事件:事件];
UIColor*normalColor=self.BackgroundColor[@(UIControlStateNormal)];
if(正常颜色){
[自转换背景颜色:正常颜色];
}
}
@结束

答案很好,但我认为这是一个有点过于复杂的解决方案。不需要对UIButton进行子类化就可以做到这一点,而且他们从来没有问过任何关于颜色动画的问题,所以我认为你的答案有点多余。答案很好,但我认为解决方案有点过于复杂。不需要对UIButton进行子类化就可以做到这一点,而且他们从来没有问过任何关于颜色动画的问题,所以我认为你的答案中的部分有点多余。这个答案假设提问者想要一个纯色作为整个按钮。他要求的是按州改变背景色,例如,可以使图标根据背景色改变不同的颜色。这个答案假设询问者想要一个纯色作为整个按钮。他要求的是按州改变背景色,例如,可以使图标根据背景色改变不同的颜色。