标签:
1
2
3
4
5
6
7
|
//创建一个ContactAdd类型的按钮 let button: UIButton = UIButton (type:. ContactAdd ) //设置按钮位置和大小 button.frame= CGRectMake (10,
150, 100, 30) //设置按钮文字 button.setTitle( "按钮" ,
forState: UIControlState . Normal ) self .view.addSubview(button); |
1
|
let button
= UIButton (frame: CGRectMake (10,
150, 100, 30)) |
1
2
3
|
button.setTitle( "普通状态" ,
forState: UIControlState . Normal ) //普通状态下的文字 button.setTitle( "触摸状态" ,
forState: UIControlState . Highlighted ) //触摸状态下的文字 button.setTitle( "禁用状态" ,
forState: UIControlState . Disabled ) //禁用状态下的文字 |
1
2
3
|
button.setTitleColor( UIColor .blackColor(),forState:
. Normal ) //普通状态下文字的颜色 button.setTitleColor( UIColor .greenColor(),forState:
. Highlighted ) //触摸状态下文字的颜色 button.setTitleColor( UIColor .grayColor(),forState:
. Disabled ) //禁用状态下文字的颜色 |
1
2
3
|
button.setTitleShadowColor( UIColor .greenColor(),forState:. Normal ) //普通状态下文字阴影的颜色 button.setTitleShadowColor( UIColor .yellowColor(),forState:. Highlighted ) //普通状态下文字阴影的颜色 button.setTitleShadowColor( UIColor .grayColor(),forState:. Disabled ) //普通状态下文字阴影的颜色 |
1
|
button.backgroundColor= UIColor .blackColor() |
1
2
3
|
button.setImage( UIImage (named: "icon1" ),forState:. Normal ) //设置图标 button.adjustsImageWhenHighlighted= false //使触摸模式下按钮也不会变暗 button.adjustsImageWhenDisabled= false //使禁用模式下按钮也不会变暗 |
1
|
button.setBackgroundImage( UIImage (named: "background1" ),forState:. Normal ) |
1
2
3
4
5
6
7
8
9
10
11
|
//不传递触摸对象(即点击的按钮) button.addTarget( self ,action:#selector(tapped),forControlEvents:. TouchUpInside ) func tapped(){ print ( "tapped" ) } //传递触摸对象(即点击的按钮),需要在定义action参数时,方法名称后面带上冒号 button.addTarget( self ,action:#selector(tapped(_:)),forControlEvents:. TouchUpInside ) func tapped(button: UIButton ){ print (button.titleForState(. Normal )) |
标签:
原文地址:http://blog.csdn.net/u012131827/article/details/51372513