Swift4.0 便利建構函式的寫法

ZY_FlyWay發表於2018-02-02

在UIButton 類擴充套件中新增新的便利建構函式如下:

extension UIButton {
    
    convenience init(composeForeImageName:String,composeBackImageName:String){
        self.init()
        setImage(UIImage.init(named: composeForeImageName), for: .normal)
        setBackgroundImage(UIImage.init(named: composeBackImageName), for: .normal)
        setBackgroundImage(UIImage.init(named: composeBackImageName+"_highlighted"), for: .selected)
        sizeToFit()
    }
    
}


呼叫:呼叫的時候就會出現新的建構函式,方便便捷。

 

  //MARK:屬性 懶載入
    lazy var composeBtn = UIButton(composeForeImageName: "tabbar_compose_icon_add", composeBackImageName: "tabbar_compose_button")



相關文章