Swift - 按钮(UIButton)的用法
1,按钮的创建
(1)按钮有下面四种类型:
(2)对于Custom定制类型按钮,代码可简化为:
1
2,按钮的文字设置
3
3,按钮文字颜色的设置
button.setTitleColor(
4,按钮文字阴影颜色的设置
button.setTitleShadowColor(
5,按钮背景颜色设置
button.backgroundColor=
6,按钮文字图标的设置
button.setimage(
7,设置按钮背景图片
button.setBackgroundImage(
8,按钮触摸点击事件响应
7
(1)按钮有下面四种类型:
UIButtonType.ContactAdd:前面带“+”图标按钮,默认文字颜色为蓝色,有触摸时的高亮效果
UIButtonType.Detaildisclosure:前面带“!”图标按钮,默认文字颜色为蓝色,有触摸时的高亮效果
UIButtonType.System:前面不带图标,默认文字颜色为蓝色,有触摸时的高亮效果
UIButtonType.Custom:定制按钮,前面不带图标,默认文字颜色为白色,无触摸时的高亮效果
UIButtonType.InfoDark:为感叹号“!”圆形按钮
UIButtonType.InfoLight:为感叹号“!”圆形按钮
|
1
2
3
4
5
6
7
|
//创建一个ContactAdd类型的按钮
let
button:
UIButton
=
(type:.
ContactAdd
)
//设置按钮位置和大小
button.frame=
CGRectMake
(10,150,100,30)
//设置按钮文字
button.setTitle(
"按钮"
,forState:
UIControlState
.
normal
)
self
.view.addSubview(button);
|
button =
(frame:
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.5em!important; margin:0px!important; overflow:visible!important; padding:1px 0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,30))
|
"普通状态"
)
//普通状态下的文字
"触摸状态"
Highlighted
//触摸状态下的文字
"禁用状态"
disabled
//禁用状态下的文字
|
| //不传递触摸对象(即点击的按钮)
button.addTarget(
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.5em!important; margin:0px!important; overflow:visible!important; padding:1px 0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,action:#selector(tapped),forControlEvents:.
TouchUpInside
func
tapped(){
print
(
"tapped"
)
}
//传递触摸对象(即点击的按钮),需要在定义action参数时,方法名称后面带上冒号
)
tapped(button:
){
(button.titleForState(.
))
|