便利构造器

2017-07-31  本文已影响0人  Rayla_周周

类方法

extension UIBarButtonItem {
     class func createItem(imageName:String,highImageName : String,size : CGSize) -> UIBarButtonItem {
     let btn = UIButton()
     btn.setImage(UIImage.init(named: imageName), for: .normal)
     btn.setImage(UIImage.init(named: highImageName), for: .highlighted)
     btn.frame = CGRect(origin: CGPoint(), size: size)
     return UIBarButtonItem(customView: btn);
     }
 let size = CGSize(width: 40, height: 40)
 let historyItem = UIBarButtonItem.createItem(imageName: "image_my_history", highImageName: "Image_my_history_click", size: size)

构造方法

 convenience init(imageName:String,highImageName : String = "",size : CGSize) {
        let btn = UIButton()
        btn.setImage(UIImage.init(named: imageName), for: .normal)
        if highImageName != "" {
            /// 设置默认值
            btn.setImage(UIImage.init(named: highImageName), for: .highlighted)
        }
        btn.frame = CGRect(origin: CGPoint(), size: size)
        self.init(customView:btn)
    }
 let size = CGSize(width: 40, height: 40)
 let historyItem = UIBarButtonItem(imageName: "image_my_history", highImageName: "Image_my_history_click", size: size)
上一篇下一篇

猜你喜欢

热点阅读