How to create a button programmatically?
//创建一个button
<pre>
let button = UIButton.buttonWithType(UIButtonType.System) as! UIButton
button.frame = CGRectMake(100, 100, 100, 50)
button.backgroundColor = UIColor.blueColor()
button.setTitle("test button", forState: UIControlState.Normal)
button.addTarget(self, action:"buttonAction:", forControlEvents: UIControlEvents.TouchUpInside)
self.view.addSubview(button)
</pre>
//创建一个label
<pre>
let label = UILabel()
label.frame = CGRectMake(CGRectGetMinX(button.frame), CGRectGetMaxY(button.frame)+20, 200, 21)
label.backgroundColor = UIColor.redColor()
label.textAlignment = NSTextAlignment.Center
label.text = "请叫我老大 哼哼。。"
self.view.addSubview(label)
</pre>
//创建UITextField
<pre>
var textField: UITextField = UITextField()
textField.frame = CGRectMake(CGRectGetMinX(label.frame), CGRectGetMaxY(label.frame) + 20, 100, 50)
textField.backgroundColor = UIColor.grayColor()
self.view.addSubview(textField)
</pre>
网友评论