3

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 
}
Tunaki
  • 132,869
  • 46
  • 340
  • 423
Edgar Georgel
  • 602
  • 1
  • 7
  • 17
  • Did you try the "Convert to Latest Swift Syntax" option in Xcode? It should solve your problem automatically. – Martin R Jun 19 '16 at 09:36

1 Answers1

9

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.

Sulthan
  • 128,090
  • 22
  • 218
  • 270
  • Or `UIControlState.Normal` . – Mtoklitz113 Jun 19 '16 at 09:19
  • 1
    @Dershowitz123 I decided to use the the newer syntax although I am not a big fan of `for:` over `forState:`. – Sulthan Jun 19 '16 at 09:24
  • Yeah your answer is right! I just wanted to let him know it can done this way also. – Mtoklitz113 Jun 19 '16 at 09:25
  • 1
    @Dershowitz123 Actually my answer was wrong. You have to use `[]` for `.normal`. `UIControlState.Normal` is not available any more in Swift 3. – Sulthan Jun 19 '16 at 09:38
  • You can use .normal – Joe Ginley Jul 24 '18 at 04:50
  • @JoeGinley Yes, I can use it now. It was not possible back in Swift 3, that's why this question appeared in the first place. – Sulthan Jul 24 '18 at 06:10
  • @Sulthan It has been available since Swift 3, look here: https://stackoverflow.com/questions/37800342/uicontrolstate-normal-is-unavailable Unless they kept changing it in Swift 3 from Xcode beta or something, maybe I didn't realize. – Joe Ginley Jul 25 '18 at 21:06
  • 1
    @JoeGinley Can't really remember which version was that :) In some version the `.normal` option was missing. – Sulthan Jul 25 '18 at 21:59