以前,对于UIButton
实例,您能够为UIControlState.Normal
或setTitle
传递setImage
。 .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的
UIButton
和UIControl
更改相关的重复问题泛滥) 最佳答案
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: [])
关于ios - UIControlState.Normal不可用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37800342/