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状态,继续保持当前状态
相关推荐
wjm0410062 小时前
ios八股文 -- Objective-c
开发语言·ios·objective-c
麦兜*11 小时前
Swift + Xcode 开发环境搭建终极指南
开发语言·ios·swiftui·xcode·swift·苹果vision pro·swift5.6.3
Digitally16 小时前
重置iPhone会删除所有内容吗? 详细回答
ios·iphone
普罗米拉稀18 小时前
Flutter 复用艺术:Mixin 与 Abstract 的架构哲学与线性化解密
flutter·ios·面试
kymjs张涛1 天前
零一开源|前沿技术周刊 #12
ios·google·github
2501_915918411 天前
iOS 应用上架全流程实践,从开发内测到正式发布的多工具组合方案
android·ios·小程序·https·uni-app·iphone·webview
笔沫拾光1 天前
iOS 正式包签名指南
flutter·ios·ios签名
Magnetic_h2 天前
【iOS】锁的原理
笔记·学习·macos·ios·objective-c·cocoa·xcode
Digitally2 天前
将 iPhone 联系人转移到 Infinix 的完整指南
ios·cocoa·iphone
imLix3 天前
RunLoop 实现原理
前端·ios