标签:
|
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