

UIButton 状态小结
source link: https://www.tuicool.com/articles/uieUVfj
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

我们先从一个问题说起,小明同学接到产品的一个新需求:实现一个 UIButton
,要求在 normal、selected、highlighted 三种状态下展示不同文案。这简直太简单了,小明同学1分钟不到就实现了,关键代码如下:
[button setTitle:@"normal" forState:UIControlStateNormal]; [button setTitle:@"highlighted" forState:UIControlStateHighlighted]; [button setTitle:@"selected" forState:UIControlStateSelected];
这段代码有什么问题吗?
的确有问题!当 button
状态为 selected
时,点击按钮,文案展示 normal
,当前状态应该是 UIControlStateHighlighted
,怎么展示了 UIControlStateNormal
的文案?
问题分析
UIButton
有5种状态,分别是:default(normal), highlighted, focused, selected, disabled,通过属性 state
可以拿到当前状态值 UIControlState
,定义如下:
@property(nonatomic,readonly) UIControlState state; // could be more than one state (e.g. disabled|selected). synthesized from other flags. 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) UIControlStateFocused NS_ENUM_AVAILABLE_IOS(9_0) = 1 << 3, // Applicable only when the screen supports focus };
看到位运算我们就可以猜到这个状态可以组合,而且有句很关键的注释:
could be more than one state (e.g. disabled|selected). synthesized from other flags.
state 不是单独的状态,可以是多种状态的混合。
我们在回到上面的小明同学遇到的问题,当 button
状态为 selected
时,点击按钮,按钮此时的状态其实是 UIControlStateHighlighted | UIControlStateSelected
。在 官方文档
中,我们可以看到这么一句话:
If a title is not specified for a state, the default behavior is to use the title associated with the UIControlStateNormal state
也就是说小明同学其实漏设置了一种状态,然后系统展示了按钮的默认状态 UIControlStateNormal
,按钮状态并不是是 UIControlStateNormal
。
小结
1、 UIControlState
是个组合状态,在 setTitle:forState:
、 setImage:forState:
时,如果需要自定义 UIControlStateSelected
状态,一定要注意自定义 UIControlStateHighlighted | UIControlStateSelected
。
2、如果按钮的当前状态没有自定义,使用 UIControlStateNormal
定义的值。
Recommend
-
103
website upgrading… 京ICP备110065...
-
34
README.md NGUIButtonInsetsExample Example app which shows how UIButton insets work
-
10
iOS 14.0 brings us many improvements and new APIs using which the developers can write efficient code. One such API is the closure based action API instead of the addTarget method in UIControls. Yes, you heard me right—no more us...
-
9
云音乐大前端专栏UIButton 状态新解2020-10-14...
-
8
跳出手掌心——如何立即触发UIButton边界事件 21 May 2015 • 5 min. read • 2 Comments
-
38
A new way to style UIButton with UIButton.Configuration in iOS 15 17 Jun 2021 ⋅ 12 min read ⋅ iOS 15
-
9
Customizing UIButton in iOS 15Buttons are an essential element in iOS apps. If you are developing an app using UIKit, you will probably use UIButton class to create buttons. Creating a button is a straightforward process, but it becomes prob...
-
4
如何扩展 Combine:以给 UIButton 设置 title 为例 我的 PasteNow 用了 Apple 自带的 Combine 库来实现一些响应...
-
54
How to make a custom button style with UIButton.Configuration in iOS 15 13 Sep 2021 ⋅ 18 min read ⋅ iOS 15
-
6
iOS SDK: Difference between UIButton setTitleForState and UIButton titleLabel.text advertisements I have this issue with a custom UIView where...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK