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

设置按钮的边框

    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);

设置按钮的文字样式

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

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

设置按钮的背景颜色

    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];

设置按钮的文字内容

  [self.titleBtn setTitle:title forState:UIControlStateNormal];
  [self.titleBtn setTitle:title forState:UIControlStateSelected];

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

 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状态,继续保持当前状态

相关推荐

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

    2024-04-24 07:38:04       34 阅读
  2. C#WPF设置圆角按钮样式

    2024-04-24 07:38:04       33 阅读
  3. wpf-动态设置组件【按钮为例】样式

    2024-04-24 07:38:04       55 阅读
  4. uniapp中各种状态按钮

    2024-04-24 07:38:04       68 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-24 07:38:04       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-24 07:38:04       106 阅读
  3. 在Django里面运行非项目文件

    2024-04-24 07:38:04       87 阅读
  4. Python语言-面向对象

    2024-04-24 07:38:04       96 阅读

热门阅读

  1. 利用HttpClient库下载蚂蜂窝图片

    2024-04-24 07:38:04       35 阅读
  2. Ant Design Pro + springboot实现文件上传功能

    2024-04-24 07:38:04       33 阅读
  3. 算法设计与优化——向量中数据唯一化

    2024-04-24 07:38:04       32 阅读
  4. K8s: 控制器之StatefulSets对象

    2024-04-24 07:38:04       29 阅读
  5. flutter 解决ExpandableText组件三个点调整颜色问题

    2024-04-24 07:38:04       31 阅读