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;
}
posted @   wjwdive  阅读(219)  评论(0编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· AI 智能体引爆开源社区「GitHub 热点速览」
· Manus的开源复刻OpenManus初探
· 写一个简单的SQL生成工具
历史上的今天:
2017-07-11 python3学习笔记(7)_listComprehensions-列表生成式
2017-07-11 python3学习笔记(6)_iteration
点击右上角即可分享
微信分享提示