UIControlState.Normal is Unavailable

11,847

Solution 1

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: [])

Solution 2

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.

Solution 3

Apple brought back the normal control state in more recent versions of Xcode beta. Upgrade to the most recent Xcode beta and use .normal.

Solution 4

Swift 5

Replace from

btn.setTitle("title", for: .Normal)

to

btn.setTitle("title", for: UIControl.State.normal)
Share:
11,847
JAL
Author by

JAL

iOS/tvOS video engineer living in San Francisco. You can find me in the SOCVR or in SOBotics as a room owner.

Updated on June 05, 2022

Comments

  • JAL
    JAL almost 2 years

    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)

  • Gary Makin
    Gary Makin almost 8 years
    If you were to add those extensions, you should have a lower-case n to start normal to match the other values like highlighted and selected.
  • Galvin
    Galvin almost 8 years
    @GaryMakin Your right, but I provide this extension for the guy who doesn't want to replace all in files. because the original one is .Normal so I keep it N not n. I think who need to the n they just know how to change it. This is just an example and we all developer not common user, right?
  • JAL
    JAL almost 8 years
    Although this initializer is exposed through OptionSetType, I'm not sure I would recommend this approach. Personally, I think [] is more readable, but this is valid nonetheless.
  • Allison
    Allison almost 8 years
    Mhmm [] is most certainly the cleanest and most obvious way to do this. Glad Apple decided to do this for some reason /s. I hope this is just a beta sorta thing but it doesn't look like that.
  • DawnSong
    DawnSong over 7 years
    UIControlState() is better than UIControlState(rawValue:UInt(0)). Practically, normal should be no state, so normal is removed.
  • DawnSong
    DawnSong over 7 years
    When using Xcode 8.1, convertor give UIControlState() to substitute .Normal.
  • Josh
    Josh almost 7 years
    Make up your mind Apple!! normal, or .Normal????? You are more bipolar than my ex.
  • Tim Nuwin
    Tim Nuwin almost 7 years
    I'm on my 5th hour in converting this project from swift 2 to 3. if a reasonable thought is to rebuild all of the code from scratch then you messed up. unbelievable. This wouldn't even be bad if they allowed swift 2 support on their latest xcode.
  • Tim Nuwin
    Tim Nuwin almost 7 years
    @JAL One week's not terrible for conversion!