設定button正常狀態的標題

彼得潘的 iOS App Neverland
2 min readJun 29, 2016

button有各種狀態,不過我們最常用的非normal狀態莫屬。但是,在新版的SDK裡 (搭配Xcode 8),定義button狀態的UIControlState型別竟找不到normal !

public struct UIControlState : OptionSet {public init(rawValue: UInt)public static var highlighted: UIControlState { get } // used when UIControl isHighlighted is setpublic 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 focuspublic static var application: UIControlState { get } // additional flags available for application usepublic static var reserved: UIControlState { get } // flags reserved for internal framework use}

天啊! 我們再也無法設定button的標題了 ! 當然不可能,Apple只是把事情變得更簡單,現在呼叫function setTitle(_:for:)時,只要在for參數傳入[ ]即可設定button正常狀態的標題。

let button = UIButton()button.setTitle("愛瘋一切為蘋果的彼得潘", for: [])

--

--

彼得潘的 iOS App Neverland

彼得潘的iOS App程式設計入門,文組生的iOS App程式設計入門講師,彼得潘的 Swift 程式設計入門,App程式設計入門作者,http://apppeterpan.strikingly.com