swift中类方法创建button等等其他控件
2017-04-25 17:46
363 查看
// // QYPButton.swift // Lease // // Created by Apple on 2017/3/16. // Copyright © 2017年 Apple. All rights reserved. // import UIKit extension UIButton { class func createButton(normalTitle:String,selectedTitle:String,frame:CGRect,tag:Int,action:Selector ,target:Any?) -> UIButton { //1:创建button let button = UIButton() //2:设置按钮的bg图片与普通图片 button.frame = frame button.setTitle(normalTitle, for: .normal) button.setTitleColor(#colorLiteral(red: 0.2196078449, green: 0.007843137719, blue: 0.8549019694, alpha: 1), for: .normal) button.setTitle(selectedTitle, for: .selected) button.setTitleColor(#colorLiteral(red: 0.8078431487, green: 0.02745098062, blue: 0.3333333433, alpha: 1), for: .selected) button.titleLabel?.font = UIFont.systemFont(ofSize: 12) button.tag = tag button.addTarget(target, action: action, for: .touchUpInside) //4:返回按钮 return button } class func createButton(image:UIImage,imageSelect:UIImage,tag:Int,action:Selector ,target:Any?) -> UIButton { //1:创建button let button = UIButton() //2:设置按钮的bg图片与普通图片 button.setImage(image, for: .normal) button.setImage(imageSelect, for: .normal) button.titleLabel?.font = UIFont.systemFont(ofSize: 12) button.tag = tag button.addTarget(target, action: action, for: .touchUpInside) //4:返回按钮 return button } //MARK:-2:提供对象方法:在构造函数的对象方法中,self就是当前调用方法的对象,所以不用再去创建对象 convenience init( _ normalTitle:String,selectedTitle:String,frame:CGRect,tag:Int,action:Selector) { //1:必须首先调用self.init() self.init() //1:创建button let button = UIButton() //2:设置按钮的bg图片与普通图片 button.frame = frame button.setTitle(normalTitle, for: .normal) button.setTitleColor(#colorLiteral(red: 0.2196078449, green: 0.007843137719, blue: 0.8549019694, alpha: 1), for: .normal) button.setTitle(selectedTitle, for: .selected) button.setTitleColor(#colorLiteral(red: 0.8078431487, green: 0.02745098062, blue: 0.3333333433, alpha: 1), for: .selected) button.titleLabel?.font = UIFont.systemFont(ofSize: 12) button.tag = tag button.addTarget(self, action: action, for: .touchUpInside) } }
调用的时候通过文件类名就可以直接调用了
相关文章推荐
- winform中button点击后再点击其他控件致使button失去焦点,此时button出现黑色边线,去掉黑色边线的方法
- Swift-常用控件创建(UIAlertController,UITableView)等等
- 动态创建View的方法,包括Button等等
- ASP.NET一个页面多个Button按钮事件避免数据验证控件RequiredFieldValidator冲突方法
- 关于如何操作其他窗体的控件或变量的方法!
- 处理拖动的时候,窗口出现重影和其他窗口残余控件的方法
- 创建一个自定义WebControl控件 WebImageButton的过程
- 如何用C#创建一个类并在其他Web页中调用该类的方法(2)
- asp.net 导出excel时报类型“DataGridLinkButton”的控件“dgExcel__ctl1__ctl0”必须放在具有 runat=server 的窗体标记内 解决方法
- 获取其他窗口中控件的方法
- 错误:在一个线程上创建的控件不能成为在另一个线程控件的父级(处理方法)
- [VB.NET]反射问题,怎么通过动态创建的控件得到控件的方法!
- 关于如何操作其他窗体的控件或变量的方法
- 在Repeater中使用按钮或其他控件的解决方法
- (转)ASP.NET一个页面多个Button按钮事件避免数据验证控件RequiredFieldValidator冲突方法
- 动态创建“WEB用户控件(WEB User Control)”又一方法,及如何调用Web用户控件的方法,如何对控件用户控件中的控件
- 创建某控件的线程之外的其他线程试图调用该控件引发的问题
- C#.net中web工程中创建用户控件和被调用的方法
- “线程间操作无效:从不是创建控件“XX”的线程访问它”的解决方法
- “从不是创建XX控件的线程访问它”错误的解决方法!