How to create a button programmatically?
Here is a complete solution to add a UIButton
programmatically with the targetAction.
Swift 2.2
override func viewDidLoad() { super.viewDidLoad() let button = UIButton(frame: CGRect(x: 100, y: 100, width: 100, height: 50)) button.backgroundColor = .greenColor() button.setTitle("Test Button", forState: .Normal) button.addTarget(self, action: #selector(buttonAction), forControlEvents: .TouchUpInside) self.view.addSubview(button)}func buttonAction(sender: UIButton!) { print("Button tapped")}
It is probably better to use NSLayoutConstraint
rather than frame
to correctly place the button for each iPhone screen.
Updated code to Swift 3.1:
override func viewDidLoad() { super.viewDidLoad() let button = UIButton(frame: CGRect(x: 100, y: 100, width: 100, height: 50)) button.backgroundColor = .green button.setTitle("Test Button", for: .normal) button.addTarget(self, action: #selector(buttonAction), for: .touchUpInside) self.view.addSubview(button)}func buttonAction(sender: UIButton!) { print("Button tapped")}
Updated code to Swift 4.2:
override func viewDidLoad() { super.viewDidLoad() let button = UIButton(frame: CGRect(x: 100, y: 100, width: 100, height: 50)) button.backgroundColor = .green button.setTitle("Test Button", for: .normal) button.addTarget(self, action: #selector(buttonAction), for: .touchUpInside) self.view.addSubview(button)}@objc func buttonAction(sender: UIButton!) { print("Button tapped")}
The above still works if func buttonAction
is declared private
or internal
.
You can add UIButton,UIlable and UITextfield programmatically in this way.
UIButton code
// var button = UIButton.buttonWithType(UIButtonType.System) as UIButtonlet button = UIButton(type: .System) // let preferred over var herebutton.frame = CGRectMake(100, 100, 100, 50)button.backgroundColor = UIColor.greenColor()button.setTitle("Button", forState: UIControlState.Normal)button.addTarget(self, action: "Action:", forControlEvents: UIControlEvents.TouchUpInside)self.view.addSubview(button)
UILabel Code
var label: UILabel = UILabel()label.frame = CGRectMake(50, 50, 200, 21)label.backgroundColor = UIColor.blackColor()label.textColor = UIColor.whiteColor()label.textAlignment = NSTextAlignment.Centerlabel.text = "test label"self.view.addSubview(label)
UITextField code
var txtField: UITextField = UITextField()txtField.frame = CGRectMake(50, 70, 200, 30)txtField.backgroundColor = UIColor.grayColor()self.view.addSubview(txtField)
Hope this is helpful for you.
For Swift 3
let button = UIButton()button.frame = CGRect(x: self.view.frame.size.width - 60, y: 60, width: 50, height: 50)button.backgroundColor = UIColor.redbutton.setTitle("your Button Name", for: .normal)button.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)self.view.addSubview(button)func buttonAction(sender: UIButton!) { print("Button tapped")}
For Swift 4+
let button = UIButton() button.frame = CGRect(x: self.view.frame.size.width - 60, y: 60, width: 50, height: 50) button.backgroundColor = UIColor.red button.setTitle("Name your Button ", for: .normal) button.addTarget(self, action: #selector(buttonAction), for: .touchUpInside) self.view.addSubview(button) @objc func buttonAction(sender: UIButton!) { print("Button tapped") }