Swift_UIButton
1.初始化
/**
UIButtonType.system:前面不带图标,默认文字颜色为蓝色,有触摸时的高亮效果
UIButtonType.custom:定制按钮,前面不带图标,默认文字颜色为白色,无触摸时的高亮效果
UIButtonType.contactAdd:前面带“+”图标按钮,默认文字颜色为蓝色,有触摸时的高亮效果
UIButtonType.detailDisclosure:前面带“!”图标按钮,默认文字颜色为蓝色,有触摸时的高亮效果
UIButtonType.infoDark:为感叹号“!”圆形按钮
UIButtonType.infoLight:为感叹号“!”圆形按钮
(注意:自ios7起,infoDark、infoLight、detailDisclosure效果都是一样的)
*/
let buttonOne = UIButton.init(type: UIButtonType.custom)
2. 设置
/**
normal
highlighted
disabled
selected
*/
buttonOne.setTitle("这个是一个button", for: UIControlState.normal)
buttonOne.setImage(UIImage.init(named: "1.jpg"), for: UIControlState.normal) // 设置背景颜色
buttonOne.setBackgroundImage(UIImage.init(named: ""), for: UIControlState.normal) // 点击事件
buttonOne.addTarget(self, action: #selector(buttonOneClicked), for: UIControlEvents.touchUpInside)
3.代码
import UIKit
let kScreenWidth = UIScreen.main.bounds.size.width
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.addButtonOne()
}
func addButtonOne() {
self.view.addSubview(buttonOne)
}
func buttonOneClicked() {
print("点击了")
}
// 懒加载一个UIButton
lazy var buttonOne: UIButton = {
/**
UIButtonType.system:前面不带图标,默认文字颜色为蓝色,有触摸时的高亮效果
UIButtonType.custom:定制按钮,前面不带图标,默认文字颜色为白色,无触摸时的高亮效果
UIButtonType.contactAdd:前面带“+”图标按钮,默认文字颜色为蓝色,有触摸时的高亮效果
UIButtonType.detailDisclosure:前面带“!”图标按钮,默认文字颜色为蓝色,有触摸时的高亮效果
UIButtonType.infoDark:为感叹号“!”圆形按钮
UIButtonType.infoLight:为感叹号“!”圆形按钮
(注意:自ios7起,infoDark、infoLight、detailDisclosure效果都是一样的)
*/
let buttonOne = UIButton.init(type: UIButtonType.custom)
buttonOne.frame = CGRect.init(x: , y: , width: kScreenWidth - , height: )
buttonOne.backgroundColor = UIColor.red
/**
normal
highlighted
disabled
selected
*/
buttonOne.setTitle("这个是一个button", for: UIControlState.normal)
buttonOne.setImage(UIImage.init(named: "1.jpg"), for: UIControlState.normal)
// 设置背景颜色
buttonOne.setBackgroundImage(UIImage.init(named: ""), for: UIControlState.normal)
// 点击事件
buttonOne.addTarget(self, action: #selector(buttonOneClicked), for: UIControlEvents.touchUpInside)
return buttonOne
}()
}
Swift_UIButton的更多相关文章
随机推荐
- [Leetcode] unique paths ii 独特路径
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- JUnit4.11 理论机制 @Theory 完整解读
最近在研究JUnit4,大部分基础技术都是通过百度和JUnit的官方wiki学习的,目前最新的发布版本是4.11,结合代码实践,发现官方wiki的内容或多或少没有更新,Theory理论机制章节情况尤为 ...
- Fabric证书解析
一.证书目录解析 通过cryptogen生成所有证书文件后,以peerOrgannizations的第一个组织树org1为例,每个目录和对应文件的功能如下: ca: 存放组织的根证书和对应的私 ...
- mmall项目之问题一(mavenplugin问题)
在进行mybatis逆向工程到时候,报错,提示maven plugin 错误,提示missing..... 解决办法: 因为之前到pom中忘记了加版本信息,添加后错误消失:
- classList详解,让你的js方便地操作DOM类
在此之前,jQuery的hasClass.addClass.removeClass我们已经再熟悉不过了,然而我们并不会在每一个项目中都会去使用 jQuery或者Zepto,譬如在移动端的网页中,考虑到 ...
- php连接mysql报错——Fatal error: Call to undefined function mysql_connect() in
练习php连接mysql数据库 代码:mysql_connect("127.0.0.1:3306","root", ..... 浏览器报错:Fatal erro ...
- 51nod 1020 逆序排列——dp
在一个排列中,如果一对数的前后位置与大小顺序相反,即前面的数大于后面的数,那么它们就称为一个逆序.一个排列中逆序的总数就称为这个排列的逆序数. 如2 4 3 1中,2 1,4 3,4 1,3 1是逆序 ...
- HDU 1162 Eddy's picture (最小生成树 普里姆 )
题目链接 Problem Description Eddy begins to like painting pictures recently ,he is sure of himself to be ...
- 渗透测试中如何科学地使用V*P*N
环境说明 Windows7 虚拟机,作为VPN网关,负责拨VPN.VPN可以直接OPENVPN,也可以使用ShadowSocks+SSTap. Kali 虚拟机, 渗透测试工作机 配置步骤 Windo ...
- mongoDB文档操作【增删改】
MongoDB 插入文档 文档的数据结构和JSON基本一样. 所有存储在集合中的数据都是BSON格式. BSON是一种类json的一种二进制形式的存储格式,简称Binary JSON. 插入文档 Mo ...