iOS 自定义一对UI表现相反的按钮

假如有一对按钮【重置】【提交】,要让他们的默认UI和点击的UI表现刚好相反
【提交】按钮,默认橙色,点击边框是橙色,字体是橙色,背景变白色
【重置】按钮,默认白色橙色,边框是橙色,点击字体是白色,背景变橙色

@implementation SubmmitButton

- (instancetype)initWithFrame:(CGRect)frame withTitle:(NSString *)title {
    self = [super initWithFrame:frame];
    if (self) {
        UIImage *image = [UIImage createImageWithColor:[UIColor orangeColor]];
//        UIImage *selectedImage = [UIImage createImageWithColor:[UIColor whiteColor]];
        self = [SubmmitButton new];
        [self setTitle:title forState:UIControlStateNormal];
        [self setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [self setBackgroundImage:image forState:UIControlStateNormal];
        [self.titleLabel setFont:[UIFont systemFontOfSize:16]];
        [self.layer setMasksToBounds:YES];
        [self.layer setCornerRadius:6];
    }
    return self;
}

- (void)setSelected:(BOOL)selected {
    [super setSelected:selected];
}

- (void)setHighlighted:(BOOL)highlighted {
    [super setHighlighted:highlighted];
    UIImage *image = [UIImage createImageWithColor:[UIColor orangeColor]];
    UIImage *selectedImage = [UIImage createImageWithColor:[UIColor whiteColor]];
    if (highlighted) {
        [self setBackgroundImage:selectedImage forState:UIControlStateHighlighted];
        [self setTitleColor:[UIColor orangeColor] forState:UIControlStateHighlighted];
        self.layer.borderWidth = 1;
        self.layer.borderColor = [UIColor orangeColor].CGColor;
    }else {
        [self setBackgroundImage:image forState:UIControlStateNormal];
        [self setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
    }
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end

@implementation RestButton
- (instancetype)initWithFrame:(CGRect)frame withTitle:(NSString *)title {
    self = [super initWithFrame:frame];
    if (self) {
//        UIImage *image = [UIImage createImageWithColor:[UIColor orangeColor]];
        UIImage *selectedImage = [UIImage createImageWithColor:[UIColor whiteColor]];
        self = [RestButton new];
        [self setTitle:title forState:UIControlStateNormal];
        [self setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
        [self setBackgroundImage:selectedImage forState:UIControlStateNormal];
        [self.titleLabel setFont:[UIFont systemFontOfSize:16]];
        [self.layer setMasksToBounds:YES];
        [self.layer setCornerRadius:6];
        self.layer.borderWidth = 1;
        self.layer.borderColor = [UIColor orangeColor].CGColor;
    }
    return self;
}

- (void)setSelected:(BOOL)selected {
    [super setSelected:selected];
}

- (void)setHighlighted:(BOOL)highlighted {
    [super setHighlighted:highlighted];
    UIImage *image = [UIImage createImageWithColor:[UIColor orangeColor]];
    UIImage *selectedImage = [UIImage createImageWithColor:[UIColor whiteColor]];
    if (highlighted) {
        [self setBackgroundImage:image forState:UIControlStateHighlighted];
        [self setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
    }else {
        [self setBackgroundImage:selectedImage forState:UIControlStateNormal];
        [self setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
        self.layer.borderWidth = 1;
        self.layer.borderColor = [UIColor orangeColor].CGColor;
    }
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end

UIImage的一个分类方法

/**
 根据颜色生图片

 @param color 生成图片的颜色
 @return 返回纯色图片
 */
+ (UIImage*)createImageWithColor:(UIColor*)color {
    CGRect rect = CGRectMake(0.0f,0.0f,10.0f,10.0f);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context=UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    UIImage *theImage=UIGraphicsGetImageFromCurrentImageContext();UIGraphicsEndImageContext();
    return theImage;
}
原文地址:https://www.cnblogs.com/wjw-blog/p/11172362.html