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状态,继续保持当前状态
相关推荐
2501_940094025 小时前
iPhone Delta模拟器游戏资源包合集中文游戏ROM+BIOS+Delta皮肤附游戏导入教程
游戏·ios·iphone
2501_915918415 小时前
HTTP和HTTPS工作原理、安全漏洞及防护措施全面解析
android·http·ios·小程序·https·uni-app·iphone
白玉cfc5 小时前
【iOS】UICollectionView
macos·ios·cocoa
2501_916007477 小时前
如何在 Windows 电脑上调试 iOS 设备上的 Safari?完整方案与实战经验分享
android·windows·ios·小程序·uni-app·iphone·safari
2501_915918417 小时前
uni-app iOS日志管理实战,从调试控制台到系统日志的全链路采集与分析指南
android·ios·小程序·https·uni-app·iphone·webview
ajassi20007 小时前
开源 Objective-C IOS 应用开发(一)macOS 的使用
macos·ios
ajassi20007 小时前
开源 Objective-C IOS 应用开发(二)Xcode安装
ios·objective-c·xcode
YungFan8 小时前
SwiftUI-WebView 全面指南
ios·swiftui
猪哥帅过吴彦祖8 小时前
Flutter 从入门到精通:深入 Navigator 2.0 - GoRouter 路由完全指南
android·flutter·ios
Kapaseker10 小时前
Kotlin 跨平台开发中的权衡
android·ios·kotlin