理解 UIControl 的 State 用法

Jun 25, 2025 • 预计阅读时间 1 分钟

UIControl 组件有 normaldisabledselected 3 种状态,normalselected 状态下又有 2 种额外的状态 disabledhighlighted 状态用来表示被点击时的状态。

所以 state 一共有 6 种有效组合

  1. normal 默认状态
  2. highlighted 被点击状态
  3. disabled 禁用交互状态,这个状态下不应该有高亮了
  4. selected 已选择状态
  5. selected + highlighted 已选择 + 被点击
  6. selected + disabled 已选择 + 禁止交互,一般用于不可取消的默认选择

在 Swift 种,为 UIButton 设置状态:

let button = UIButton(type: .custom)
button.setTitle("1", for: .normal)
button.setTitle("2", for: .highlighted)
button.setTitle("3", for: .disabled)
button.setTitle("4", for: .selected)
button.setTitle("5", for: [.selected, .highlighted])
button.setTitle("6", for: [.selected, .disabled])
iOS
版权声明:如果转发请带上本文链接和注明来源。

lvv.me

iOS/macOS Developer

在 Xcode 中把 Folder 转为 Group