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

[Swift]UIKit学习之UIButton的用法

发布时间:2020-12-14 01:45:16 所属栏目:百科 来源:网络整理
导读:UIButton的创建: (1) 在Stroyboard中使用Ctrl+Drag拖拽法创建 (2) 代码创建: UIButton (frame: #T##CGRect#) //创建一个Buttonletbutton=UIButton(type:UIButtonType.Custom)//创建一个CGRect,设置位置和大小button.frame=CGRectMake(50,150,50)//设置背景


UIButton的创建:

(1) 在Stroyboard中使用Ctrl+Drag拖拽法创建

(2) 代码创建:UIButton(frame: <#T##CGRect#>)


//创建一个Button
letbutton=UIButton(type:UIButtonType.Custom)
//创建一个CGRect,设置位置和大小
button.frame=CGRectMake(50,150,50)

//设置背景色
button.backgroundColor=UIColor.greenColor()

//设置标题
button.setTitle("点击投票",forState:UIControlState.Normal)

//传递触摸对象(点击事件)
button.addTarget(self,action:"buttonPressed:",forControlEvents:UIControlEvents.TouchUpInside)

//添加子视图
self.view.addSubview(button)
//触摸事件(点击事件)
funcbuttonPressed(button:UIButton){
button.setTitle("感谢您的点击",forState:UIControlState.Normal);
}




参考链接:

https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIButton_Class/index.html

http://www.hangge.com/blog/cache/detail_529.html

(编辑:李大同)

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

    推荐文章
      热点阅读