UIControlState.Normal不可用

JAL*_*JAL 13 uibutton ios swift swift3 xcode8

此前的UIButton情况下,你能够传递UIControlState.NormalsetTitlesetImage. .Normal不再可用,我应该使用什么?

let btn = UIButton(frame: CGRect(x: 0, y: 0, width: 20, height: 20))
btn.setTitle("title", for: .Normal) // does not compile
Run Code Online (Sandbox Code Playgroud)

(这是一个规范的问答对,以防止与此相关的重复问题UIButton以及UIControliOS 10和Swift 3的变化)

JAL*_*JAL 22

Swift 3更新:

似乎Xcode 8/Swift 3带UIControlState.normal回来了:

public struct UIControlState : OptionSet {

    public init(rawValue: UInt)


    public static var normal: UIControlState { get }

    public static var highlighted: UIControlState { get } // used when UIControl isHighlighted is set

    public static var disabled: UIControlState { get }

    public static var selected: UIControlState { get } // flag usable by app (see below)

    @available(iOS 9.0, *)
    public static var focused: UIControlState { get } // Applicable only when the screen supports focus

    public static var application: UIControlState { get } // additional flags available for application use

    public static var reserved: UIControlState { get } // flags reserved for internal framework use
}
Run Code Online (Sandbox Code Playgroud)

UIControlState.Normal已重命名为UIControlState.normaliOS SDK并从中删除.对于"Normal"选项,使用空数组构造空选项集.

let btn = UIButton(frame: CGRect(x: 0, y: 0, width: 20, height: 20))

// Does not work
btn.setTitle("title", for: .Normal) // 'Normal' has been renamed to 'normal'
btn.setTitle("title", for: .normal) // 'normal' is unavailable: use [] to construct an empty option set

// Works
btn.setTitle("title", for: [])
Run Code Online (Sandbox Code Playgroud)