Swift - 按钮(UIButton)的用法
来源:互联网 发布:深圳 人工智能 编辑:程序博客网 时间:2024/06/05 09:09
Swift - 按钮(UIButton)的用法
原文地址:http://hangge.com/blog/cache/detail_529.html 作者:yuhang1,按钮的创建
(1)按钮有下面四种类型:
UIButtonType.ContactAdd:前面带“+”图标按钮,默认文字颜色为蓝色,有触摸时的高亮效果
UIButtonType.DetailDisclosure:前面带“!”图标按钮,默认文字颜色为蓝色,有触摸时的高亮效果
UIButtonType.System:前面不带图标,默认文字颜色为蓝色,有触摸时的高亮效果
UIButtonType.Custom:定制按钮,前面不带图标,默认文字颜色为白色,无触摸时的高亮效果
1
2
3
4
5
6
7
//创建一个ContactAdd类型的按钮
var
button:
UIButton
=
UIButton
.buttonWithType(
UIButtonType
.
ContactAdd
)
as
UIButton
;
//设置按钮位置和大小
button.frame=
CGRectMake
(10, 150, 100, 30);
//设置按钮文字
button.setTitle(
"按钮"
, forState:
UIControlState
.
Normal
)
self
.view.addSubview(button);
1
var
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:
UIControlEvents
.
TouchUpInside
)
func
tapped(){
println
(
"tapped"
)
}
//传递触摸对象(即点击的按钮),需要在定义action参数时,方法名称后面带上冒号
button.addTarget(
self
,action:
Selector
(
"tapped:"
),forControlEvents:
UIControlEvents
.
TouchUpInside
)
func
tapped(button:
UIButton
){
println
(button.titleForState(.
Normal
))
}
0 0
- Swift - 按钮(UIButton)的用法
- Swift - 按钮(UIButton)的用法
- Swift - 按钮(UIButton)的用法
- iOS 【Swift - 按钮(UIButton)的用法】
- Swift - 按钮(UIButton)的用法
- Swift - 按钮(UIButton)的用法
- Swift - 按钮(UIButton)的用法
- Swift - 按钮(UIButton)的用法
- Swift - 按钮(UIButton)的用法
- Swift - 按钮(UIButton)的用法
- Swift - 按钮(UIButton)的用法
- swift 3.0 按钮的用法(UIButton)
- Swift - 按钮(UIButton)的用法
- Swift - 按钮(UIButton)的用法
- swift-按钮(UIButton)用法
- Swift开发入门:按钮(UIButton)空间
- Swift-按钮(UIButton)详解
- Swift 按钮(UIButton)
- 关于opencv3.1编译的时候找不到python的lib的解决方法
- iOS项目模仿之喜马拉雅(二)—— TabBar实现
- SessionID的本质
- windows下安装mysql-5.7.11-winx64.zip后无法启动服务
- 配置Spark history server
- Swift - 按钮(UIButton)的用法
- iOS中改变选中视图的颜色等,将上次选中的视图变为默认
- JS正则表达式常用验证
- CentOS系统时间与网络同步
- ListView滑动删除 ,仿腾讯QQ
- MSSQL 查看表结构
- OkHttp的深入研究:Jesse Wilson(三)
- 第4章 Android IDA pro使用
- Android开发之静态注册实现开机启动