以前,对于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
(这是一个规范的Q& A对,用于防止与iOS 10和Swift 3中此UIButton
和UIControl
更改相关的重复问题泛滥
答案 0 :(得分:20)
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中移除。对于"正常" options,使用空数组构造一个空选项集。
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: [])
答案 1 :(得分:2)
.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
都像之前一样工作。
答案 2 :(得分:2)
Apple在更新版本的Xcode beta中恢复了正常的控制状态。升级到最新的Xcode测试版并使用.normal
。
答案 3 :(得分:0)
快速5
替换为
btn.setTitle("title", for: .Normal)
到
btn.setTitle("title", for: UIControl.State.normal)