swiftswift3ios10uicontrolstate

setTitle(_ title: String?, for state: UIControlState) where is .Normal state?


I'm updating my app to Swift 3 with iOS10 on Xcode 8 and I get an error on:

Btn.setTitle('str', forState: .Normal)

.Normal is not an enum type of UIControlState any more. Which type of UIControlState should I use for this state?

Apple enum is now defined as

public struct UIControlState : OptionSet {

   public init(rawValue: UInt)


   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 
}

Solution

  • Like any other option set:

    button.setTitle("", for: [])
    

    where [] stands for .normal (the value of .normal is 0).

    Note that we can use multiple states in one call:

    button.setTitle("", for: [.selected, .disabled])
    

    That's why UIControlState has been changed into an option set.