类方法
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)
构造方法
- 便利构造函数(推荐使用)
1. convenience开头
2. 在构造函数中必须明确调用一个设计的构造函数(self调用)
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)
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。