Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIControlState.Normal is Unavailable

Previously for UIButton instances, you were able to pass in UIControlState.Normal for setTitle or setImage. .Normal is no longer available, what should I use instead?

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

(This is a canonical Q&A pair to prevent the flood of duplicate questions related to this UIButton and UIControl changes with iOS 10 and Swift 3)

like image 534
JAL Avatar asked Jun 13 '16 22:06

JAL


People also ask

What is UIControlState?

UIControlState() is an "empty" control state with some undocumented default value. It just so happens that both of these are backed with a raw value of 0 . So it seems that both represent the "normal" state. But it is bad practice to rely on this. The defaults could change.

What is UIControl state in Swift?

static var normal: UIControl.State. The normal, or default state of a control—that is, enabled but neither selected nor highlighted. static var highlighted: UIControl.State.


2 Answers

Swift 3 update:

It appears that Xcode 8/Swift 3 brought UIControlState.normal back:

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
}

UIControlState.Normal has been renamed to UIControlState.normal and removed from the iOS SDK. For "Normal" options, use an empty array to construct an empty option set.

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: [])
like image 85
JAL Avatar answered Sep 27 '22 19:09

JAL


The .Normal is removed(iOS 10 DP1), you can use the [] or UIControlState(rawValue: UInt(0)) to replace the .Normal, if you don't want to change codes all around(in case apple add it again or you don't like the []), you can just add once this code

extension UIControlState {
    public static var Normal: UIControlState { return [] }
}

or

extension UIControlState {
    public static var Normal: UIControlState { return UIControlState(rawValue: UInt(0)) }
}

then all the .Normal work like before.

like image 27
Galvin Avatar answered Sep 27 '22 19:09

Galvin