天天看点

UIButton之纯Swift创建及其常见方法

创建UIButton实例对象:

let button = UIButton(type: UIButtonType.system)
// let button = UIButton(type: .system)
           

上述构造方法可选值:

type: UIButtonType.system

type: UIButtonType.close

type: UIButtonType.contackAdd

type: UIButtonType.custom

type: UIButtonType.detailDisclosure

type: UIButtonType.infoDark

type: UIButtonType.infoLight

type: UIButtonType.roundRect

type: UIButtonType.system

设置背景颜色:

button.backgroundColor = UIColor.purple
// button.backgroundColor = UIColor.init(white:0.9, alpha: 1)
           

设置标题:

正常状态

高亮状态

选中状态

设置标题颜色:

正常状态

高亮状态

选中状态

设置背景图片:

正常状态

高亮状态

选择状态

设置文字阴影:

普通状态

高亮状态

选中状态

按钮内容的边距(顶部,左边,底部,左边):

按钮上图片的边距:

按钮上文本框的边距:

高亮状态下调整图片:

高亮状态下变灰:

设置按钮上的图片: