UIButton的创建:
(1) 在Stroyboard中使用Ctrl+Drag拖拽法创建
(2) 代码创建:UIButton(frame: <#T##CGRect#>)
//创建一个Button
let button = UIButton(type: UIButtonType.Custom)
//创建一个CGRect, 设置位置和大小
button.frame = CGRectMake(50, 150, 150, 50)
//设置背景色
button.backgroundColor = UIColor.greenColor()
//设置标题
button.setTitle("点击投票", forState: UIControlState.Normal)
//传递触摸对象(点击事件)
button.addTarget(self,
action: "buttonPressed:",
forControlEvents: UIControlEvents.TouchUpInside)
//添加子视图
self.view.addSubview(button)
// 触摸事件(点击事件)
func buttonPressed(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