IOS 设置UIButton按钮的选中状态样式

设置按钮的边框

objectivec 复制代码
    self.titleBtn.backgroundColor = UIColor.whiteColor;
    self.titleBtn.layer.borderColor = [UIColor colorWithHexString:@"#B3B3B3" withAlpha:0.3].CGColor;
    self.titleBtn.layer.borderWidth = 0.5;
    
    self.titleBtn.clipsToBounds = YES;
    self.titleBtn.height = 48;
    self.titleBtn.size = CGSizeMake(CLSCREENWIDTH, 48);

设置按钮的文字样式

objectivec 复制代码
    self.titleBtn.tintColor = UIColor.clearColor;
    [self.titleBtn setTitleColor:[UIColor colorWithHexString:@"#2C2C2C"] forState:UIControlStateNormal];

    [self.titleBtn setTitleColor:[UIColor colorWithHexString:@"#7809FF"] forState:UIControlStateSelected];

设置按钮的背景颜色

objectivec 复制代码
    UIImage *selectImage = [UIImage imageWithColor:[UIColor colorWithHexString:@"#F5F5FF"] andSize:self.titleBtn.size];
    [self.titleBtn setBackgroundImage:selectImage forState:UIControlStateSelected];

    [self.titleBtn setTitleColor:UIColor.whiteColor forState:UIControlStateDisabled];
    UIImage *disableImage = [UIImage imageWithColor:[UIColor colorWithHexString:@"#B3B3B3"] andSize:self.titleBtn.size];
    [self.titleBtn setBackgroundImage:disableImage forState:UIControlStateDisabled];

设置按钮的文字内容

objectivec 复制代码
  [self.titleBtn setTitle:title forState:UIControlStateNormal];
  [self.titleBtn setTitle:title forState:UIControlStateSelected];

附上按钮的各种状态及交互

objectivec 复制代码
 1.UIControlStateNormal
 1> 除开UIControlStateHighlighted、UIControlStateDisabled、UIControlStateSelected以外的其他情况,都是normal状态
 2> 这种状态下的按钮【可以】接收点击事件
 
 2.UIControlStateHighlighted
 1> 【当按住按钮不松开】或者【highlighted = YES】时就能达到这种状态
 2> 这种状态下的按钮【可以】接收点击事件
 
 3.UIControlStateDisabled
 1> 【button.enabled = NO】时就能达到这种状态
 2> 这种状态下的按钮【无法】接收点击事件
 
 4.UIControlStateSelected
 1> 【button.selected = YES】时就能达到这种状态
 2> 这种状态下的按钮【可以】接收点击事件
 
 二、让按钮无法点击的2种方法
 1> button.enabled = NO;
 *【会】进入UIControlStateDisabled状态
 
 2> button.userInteractionEnabled = NO; 
 *【不会】进入UIControlStateDisabled状态,继续保持当前状态
相关推荐
人月神话-Lee13 小时前
【图像处理】亮度与对比度——图像的线性变换
图像处理·人工智能·ios·ai编程·swift
AITOP10019 小时前
高德联合千问开源AGenUI:让Agent UI同时跑在iOS、安卓和鸿蒙上
ui·ios·开源
2501_916008891 天前
ChatGPT前端开发学习指南:Visual Studio Code与谷歌浏览器安装配置详解
ide·vscode·ios·小程序·uni-app·编辑器·iphone
MonkeyKing1 天前
iOS 图片内存优化实战:解码、downSample、纹理内存与大图展示全解析
ios
00后程序员张1 天前
iOS开发中Xcode安装不完整问题解决方案与配置指南
ide·vscode·ios·objective-c·个人开发·swift·敏捷流程
2501_915909061 天前
完整指南:如何将iOS应用上架到App Store
android·ios·小程序·https·uni-app·iphone·webview
90后的晨仔1 天前
SwiftUI 高级依赖注入:构建可测试、可扩展应用的基石
ios
90后的晨仔1 天前
SwiftUI 中的 Combine:响应式编程完全指南
ios
pop_xiaoli1 天前
【iOS】RunLoop
macos·ios·objective-c·cocoa
区块block2 天前
iOS 27 重磅开放:第三方 AI 模型自由切换,苹果生态告别封闭
人工智能·ios