import UIKitextension UIbutton { //把按钮直接不同属性的名字传进来,在这里我们传进来图片名字和标题 class func creatbutton(imagename:String,Title:String) -> UIbutton { let button = UIbutton() button.setimage(UIImage(named: imagename),forState: UIControlState.normal) button.setTitle(Title,forState: UIControlState.normal) button.TitleLabel?.Font = UIFont.systemFontOfSize(12) button.setTitlecolor(UIcolor.darkGraycolor(),forState: UIControlState.normal) button.TitleEdgeInsets = UIEdgeInsets(top: 0,left: 10,bottom: 0,right: 0) return button } }在创建按钮的地方,本来要这么写
private lazy var button: UIbutton { let btn = UIbutton() btn.setimage(UIImage(named: "icon_button"),forState: UIControlState.normal) btn.setTitle("进入",forState: UIControlState.normal) btn.TitleLabel?.Font = UIFont.systemFontOfSize(12) btn.setTitlecolor(UIcolor.darkGraycolor(),forState: UIControlState.normal) btn.TitleEdgeInsets = UIEdgeInsets(top: 0,left: 10,bottom: 0,right: 0) return btn}()现在可以这么写
private lazy var button: UIbutton = UIbutton.creatbutton("icon_button",Title: "进入")如果个别按钮需要额外加一些属性,可以这么写
private lazy var button: UIbutton { let btn = UIbutton = UIbutton.creatbutton("icon_button",Title: "进入") //红色按钮 btn.backgroundcolor = UIcolor.redcolor() return btn}()总结
以上是内存溢出为你收集整理的Swift中Category的使用全部内容,希望文章能够帮你解决Swift中Category的使用所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)