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. 【POJ 1201 Intervals】

    Time Limit: 2000MSMeamory Limit: 65536K Total Submissions: 27949Accepted: 10764 Description You are ...

  2. 【BZOJ 3144】 [Hnoi2013]切糕 真·最小割

    一开始一脸懵逼后来发现,他不就是割吗,我们只要满足条件就割就行了,于是我们把他连了P*Q*R条边,然而我们要怎样限制D呢?我们只要满足对于任意相邻的两条路,只要其有个口大于D就不行就好了因此我们只要把 ...

  3. 淡入淡出效果的js原生实现

    淡入淡出效果,在日常项目中经常用到,可惜原生JS没有类似的方法,而有时小的页面并不值得引入一个jQuery库,所以就自己写了一个,已封装, 有用得着的朋友, 可以直接使用. 代码中另附有一个设置元素透 ...

  4. shell编程 if 注意事项

    read -n 1 -p "Let's go(y or n):" if [ "$REPLY"x = "y"x -o "$REPLY ...

  5. 转:js中javascript:void(0) 真正含义

    from:http://www.jb51.net/article/71532.htm 在Javascript中void是一个操作符,该操作符指定要计算一个表达式但是不返回值. 我想使用过ajax的都常 ...

  6. swt MouseAdapter

    Exception in thread "main" java.lang.Error: Unresolved compilation problem:     The method ...

  7. [Leetcode Week5]Word Ladder

    Word Ladder题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/word-ladder/description/ Description Give ...

  8. python实战===一行代码就能搞定的事情!

    打印9*9乘法表: >>> print( '\n'.join([' '.join(['%s*%s=%-2s' % (y,x,x*y) for y in range(1,x+1)]) ...

  9. Bash Shell 下打开一个TCP / UDP SOCKET

    Bash Shell 下打开一个TCP / UDP SOCKET http://jingyan.baidu.com/article/636f38bb6166c3d6b84610d1.html

  10. easyui 在子tabs中打开新tabs(关于easyUI在子页面增加显示tabs的一个问题)

    现在说的是在子页面点个按钮也能触发增加子页面的情况. 情景是,在父页面上有个div如: <div class="easyui-tabs" id="main" ...