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的更多相关文章

随机推荐

  1. ionic2-键盘覆盖输入框和返回键问题解决方案

    http://blog.csdn.net/u012979009/article/details/52514892有遇到这个问题的去这个地址看

  2. 本地上jar命令

    1.上传到jd-release mvn deploy:deploy-file -DgroupId=com.jd.open.api -DartifactId=open-api-sdk -Dversion ...

  3. Bash 实例,第二部分

    我们先看一下处理命令行自变量的简单技巧,然后再看看 bash 基本编程结构. 接收自变量 在 介绍性文章 中的样本程序中,我们使用环境变量 "$1" 来引用第一个命令行自变量.类似 ...

  4. C# 程序Hello World

    先创建一个工程文件->选择的是console application. 然后开始写代码如下: using System; using System.Collections.Generic; us ...

  5. 小程序根据input输入,动态设置按钮的样式

    [需求]实现当手机号已填写和协议已勾选时,“立即登录”按钮变亮,按钮可点击:若有一个不满足,按钮置灰,不可点击:实现获取短信验证码,倒计时提示操作:对不满足要求内容进行toast弹窗提示. <v ...

  6. Java并发(10)- 简单聊聊JDK中的七大阻塞队列

    引言 JDK中除了上文提到的各种并发容器,还提供了丰富的阻塞队列.阻塞队列统一实现了BlockingQueue接口,BlockingQueue接口在java.util包Queue接口的基础上提供了pu ...

  7. 转:强化学习(Reinforcement Learning)

    机器学习算法大致可以分为三种: 1. 监督学习(如回归,分类) 2. 非监督学习(如聚类,降维) 3. 增强学习 什么是增强学习呢? 增强学习(reinforcementlearning, RL)又叫 ...

  8. shell 灵活设置定时任务

    #!/bin/bash step=30 #间隔的秒数,不能大于60 for (( i = 0; i < 60; i=(i+step) )); do curl #调用链接 sleep $step ...

  9. webstorm vue代码修改后不更新问题

    把 safe write 的勾去掉就行了.

  10. Laravel - Property [title] does not exist on this collection instance

    When you're using get() you get a collection. In this case you need to iterate over it to get proper ...