Swift4.0 便利构造函数的写法

2019-01-15 15:31:28 浏览数 (1)

在UIButton 类扩展中添加新的便利构造函数如下:

代码语言:javascript复制
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()
    }
    
}

调用:调用的时候就会出现新的构造函数,方便便捷。

代码语言:javascript复制
  //MARK:属性 懒加载
    lazy var composeBtn = UIButton(composeForeImageName: "tabbar_compose_icon_add", composeBackImageName: "tabbar_compose_button")

0 人点赞