加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Swift - 按钮(UIButton)的用法

发布时间:2020-12-14 02:46:27 所属栏目:百科 来源:网络整理
导读:Swift - 按钮(UIButton)的用法 原文地址: http://hangge.com/blog/cache/detail_529.html 作者: yuhang 1,按钮的创建 (1)按钮有下面四种类型: UIButtonType.ContactAdd :前面带“+”图标按钮,默认文字颜色为蓝色,有触摸时的高亮效果 UIButtonType

Swift - 按钮(UIButton)的用法

原文地址: http://hangge.com/blog/cache/detail_529.html 作者: yuhang

1,按钮的创建
(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 ;
//设置按钮位置和大小
button.frame= CGRectMake (10,150,100,30);
//设置按钮文字
button.setTitle( "按钮" ,forState: UIControlState Normal )
self .view.addSubview(button);
(2)对于Custom定制类型按钮,代码可简化为:
1
button = (frame:
2,按钮的文字设置
3
"普通状态" ) //普通状态下的文字
"触摸状态" UIControlState Highlighted //触摸状态下的文字
"禁用状态" Disabled //禁用状态下的文字
3,按钮文字颜色的设置
button.setTitleColor(UIColor.blackColor(),forState: .//普通状态下文字的颜色
button.setTitleColor( UIColor .greenColor(),0)!important; background:none!important">//触摸状态下文字的颜色
.grayColor(),0)!important; background:none!important">//禁用状态下文字的颜色
4,按钮文字阴影颜色的设置
button.setTitleShadowColor(//普通状态下文字阴影的颜色
button.setTitleShadowColor( .yellowColor(),0)!important; background:none!important">//普通状态下文字阴影的颜色
//普通状态下文字阴影的颜色
5,按钮背景颜色设置
button.backgroundColor=.blackColor()
6,按钮文字图标的设置
button.setImage(UIImage(named:"icon1"),0)!important; background:none!important">//设置图标
button.adjustsImageWhenHighlighted= false //使触摸模式下按钮也不会变暗
button.adjustsImageWhenDisabled= //使禁用模式下按钮也不会变暗
7,设置按钮背景图片
button.setBackgroundImage("background1")
8,按钮触摸点击事件响应
7
8
9
10
11
//不传递触摸对象(即点击的按钮)
button.addTarget( Selector ( "tapped" UIControlEvents TouchUpInside )
func tapped(){
println )
}
//传递触摸对象(即点击的按钮),需要在定义action参数时,方法名称后面带上冒号
"tapped:" )
tapped(button: ){
(button.titleForState(. ))
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!