美文网首页iOS专题
活用UIButton的状态设置

活用UIButton的状态设置

作者: 一剑孤城 | 来源:发表于2017-07-04 11:36 被阅读143次

前言:项目中,设置UIButton的颜色,背景来显示不同的状态是很常见的操作,比如:不可点置灰,点击高亮等等。下面,记录一下,在这方面踩的的坑。

1.UIButton可以设置4种状态

typedef NS_OPTIONS(NSUInteger, UIControlState) {
    UIControlStateNormal       = 0,
    UIControlStateHighlighted  = 1 << 0,                  // used when UIControl isHighlighted is set
    UIControlStateDisabled     = 1 << 1,
    UIControlStateSelected     = 1 << 2,                  // flag usable by app (see below)
}

UIControlState是一个options而不是枚举,也就是说,这些状态可以组合,这就可能存在相互影响的问题,这就是UIControlState的大坑。

2.简要介绍一下四种state所代表的意义:

  • UIControlStateNormal

The normal, or default state of a control—that is, enabled but neither selected nor highlighted.
代表enable,unselected,unhighlight的状态,这也是个坑,normal是一切你没有设置的特殊状态都显示normal的状态,不仅仅是这个。

  • UIControlStateHighlighted

Highlighted state of a control. A control becomes highlighted when a touch event enters the control’s bounds, and it loses that highlight when there is a touch-up event or when the touch event exits the control’s bounds.
highlight只和事件有关,比如,当进入点击范围时会高亮,出去点击范围时高亮会消失,但前提下,这个UIButton可点击,否则没有意义。

  • UIControlStateDisabled

Disabled state of a control. User interactions with disabled control have no effect and the control draws itself with a dimmed appearance to reflect that it is disabled.
这个当UIButton不可点击的时候触发

  • UIControlStateSelected

Selected state of a control. For many controls, this state has no effect on behavior or appearance.
UIButton默认selected是NO

3.重点介绍这四种状态的各种显示

UIControlState是BitMask,3 个有效的 Bit 组合其实可以拥有 8 种状态,分别是:

0 0 0 => UIControlStateNormal
0 0 1 => UIControlStateHighlighted
0 1 0 => UIControlStateDisabled
0 1 1 => UIControlStateHighlighted & UIControlStateDisabled
1 0 0 => UIControlStateSelected
1 0 1 => UIControlStateSelected & UIControlStateHighlighted
1 1 0 => UIControlStateSelected & UIControlStateDisabled
1 1 1 => UIControlStateSelected & UIControlStateDisabled & UIControlStateHighlighted

//设置UIButton的8种状态
[btn setTitle: @"Normal" forState: UIControlStateNormal];
[btn setTitle: @"Highlighted" forState: UIControlStateHighlighted];
[btn setTitle: @"Disabled" forState: UIControlStateDisabled];
[btn setTitle: @"Highlighted & Disabled" forState: UIControlStateHighlighted | UIControlStateDisabled];
[btn setTitle: @"Selected" forState: UIControlStateSelected];
[btn setTitle: @"Selected & Highlighted" forState:UIControlStateSelected | UIControlStateHighlighted];
[btn setTitle: @"Selected & Disabled" forState:UIControlStateSelected | UIControlStateDisabled];
[btn setTitle: @"Selected & Highlighted & Disabled" forState: UIControlStateSelected | UIControlStateHighlighted | UIControlStateDisabled];

//设置不同的state,看UIButton的显示
//normal
UIButton *button1 = [self getButton];
button1.center = CGPointMake(center.x, base);
[self.view addSubview: button1];

//highlight
UIButton *button2 = [self getButton];
button2.center = CGPointMake(center.x, base + space);
button2.highlighted = YES;
[self.view addSubview: button2];

//disable
UIButton *button3 = [self getButton];
button3.center = CGPointMake(center.x, base + space * 2);
button3.enabled = NO;
[self.view addSubview: button3];

//highlight & disable
UIButton *button4 = [self getButton];
button4.center = CGPointMake(center.x, base + space * 3);
button4.highlighted = YES;
button4.enabled = NO;
[self.view addSubview: button4];

//selected
UIButton *button5 = [self getButton];
button5.center = CGPointMake(center.x, base + space * 4);
button5.selected = YES;
[self.view addSubview: button5];

//selected & highlight
UIButton *button6 = [self getButton];
button6.center = CGPointMake(center.x, base + space * 5);
button6.selected = YES;
button6.highlighted = YES;
[self.view addSubview: button6];

//selected & disable
UIButton *button7 = [self getButton];
button7.center = CGPointMake(center.x, base + space * 6);
button7.selected = YES;
button7.enabled = NO;
[self.view addSubview: button7];

//highlight & disable & selected
UIButton *button8 = [self getButton];
button8.center = CGPointMake(center.x, base + space * 7);
button8.highlighted = YES;
button8.enabled = NO;
button8.selected = YES;
[self.view addSubview: button8];

有图有真相:

UIControlState.png
结果是:
1.UIControlStateHighlighted & UIControlStateDisabled**会覆盖UIControlStateHighlighted的状态 2.UIControlStateSelected & UIControlStateHighlighted & UIControlStateDisabled会覆盖UIControlStateSelected & UIControlStateHighlighted 3.也就说highlight和disable在一起,不管该UIButton是否可点,都是显示highlight和disable

总结:

1.使用UIControlState尽量精确,比如说:可点状态下已选择的高亮
2.注意高亮和不可以点击状态的组合

参考:

1.《UIButton 状态的新理解》
2.apple文档

相关文章

  • 活用UIButton的状态设置

    前言:项目中,设置UIButton的颜色,背景来显示不同的状态是很常见的操作,比如:不可点置灰,点击高亮等等。下面...

  • UIButton的背景色与状态。

    在使用UIButton是,由于UIButton具有多种状态,如下: 但是UIButton虽然提供了各种状态下设置图...

  • 导致UIButton半透明的原因

    UIButton的userInteractionEnabled设置为No的时候会使UIButton变成半透明状态

  • UI控件

    UIButton 1.注意UIbutton的selected的状态; 设置的这个状态,如果后面改变button的s...

  • 04-UIButton详细属性介绍

    1.UIButton状态: 2.UIButton类型 3.UIButton常用属性** 给按钮设置文字时,苹果文档...

  • UIButton在正常(UIControlStateNormal

    UIButton在不同状态下的设置: UIControlStateNormal 正常状态 UIControlSta...

  • iOS UIButtonTypeSystem

    通过UIButtonTypeSystem创建的UIButton,在设置UIButton的选中状态时,会看到一个默认...

  • Swift学习笔记 -- UIButton

    UIButton初始化 设置背景色 状态设置 图片设置 字体设置 点击事件 点击方法实现

  • storyboard xib 使用

    直接在XIB中设置UIButton选中状态下的image

  • UIButton

    一 设置选中状态的背景色 如果使用UIButton的setBackgroundColor:方法来设置背景,在点击该...

网友评论

    本文标题:活用UIButton的状态设置

    本文链接:https://www.haomeiwen.com/subject/nhfzcxtx.html