ios – UIControlState.Normal不可用

以前对于UIButton实例,您可以传入setTitle或set Image的UIControlState.normal.正常现在不再可用了,应该用什么呢?
let btn = UIButton(frame: CGRect(x: 0,y: 0,width: 20,height: 20))
btn.setTitle("title",for: .normal) // does not compile

(这是一个规范的Q& A对,以防止与这个UIButton和UIControl有关的重复问题的洪水与iOS 10和Swift 3的变化)

解决方法

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

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...