UIControlState.Normal不可用

以前对于UIButton实例,您可以在UIControlState.Normal传递setTitlesetImage.Normal不再可用,应该使用什么?

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

(这是一个规范的问答对,以防止与iOS 10和Swift 3相关的UIButtonUIControl更改相关的重复问题泛滥)

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 } 

UIControlState.Normal已被重命名为UIControlState.normal并从iOS SDK中删除。 对于“正常”选项,使用空数组构造一个空白选项集。

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

.Normal被删除(iOS 10 DP1),你可以使用[]UIControlState(rawValue: UInt(0))来代替.Normal ,如果你不想改变代码(苹果添加它或者你不喜欢[] ),你可以只添加一次这个代码

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

要么

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

那么所有的.Normal工作就像以前一样。

苹果在Xcode beta的更新版本中带回了正常的控制状态。 升级到最新的Xcodetesting版并使用.normal