I am trying to build UI\'s programmatically. How do I get this action working? I am developing with Swift.
Code in viewDidLoad:
over
Swift: Ui Button create programmatically
let myButton = UIButton()
myButton.titleLabel!.frame = CGRectMake(15, 54, 300, 500)
myButton.titleLabel!.text = "Button Label"
myButton.titleLabel!.textColor = UIColor.redColor()
myButton.titleLabel!.textAlignment = .Center
For Swift 3 Xcode 8.......
let button = UIButton(frame: CGRect(x: 0, y: 0, width: container.width, height: container.height))
button.addTarget(self, action: #selector(self.barItemTapped), for: .touchUpInside)
func barItemTapped(sender : UIButton) {
//Write button action here
}
Swift 5.0
let button = self.makeButton(title: "Login", titleColor: .blue, font: UIFont.init(name: "Arial", size: 18.0), background: .white, cornerRadius: 3.0, borderWidth: 2, borderColor: .black)
view.addSubview(button)
// Adding Constraints
button.heightAnchor.constraint(equalToConstant: 40).isActive = true
button.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 40).isActive = true
button.rightAnchor.constraint(equalTo: view.rightAnchor, constant: -40).isActive = true
button.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -400).isActive = true
button.addTarget(self, action: #selector(pressed(_ :)), for: .touchUpInside)
// Define commmon method
func makeButton(title: String? = nil,
titleColor: UIColor = .black,
font: UIFont? = nil,
background: UIColor = .clear,
cornerRadius: CGFloat = 0,
borderWidth: CGFloat = 0,
borderColor: UIColor = .clear) -> UIButton {
let button = UIButton()
button.translatesAutoresizingMaskIntoConstraints = false
button.setTitle(title, for: .normal)
button.backgroundColor = background
button.setTitleColor(titleColor, for: .normal)
button.titleLabel?.font = font
button.layer.cornerRadius = 6.0
button.layer.borderWidth = 2
button.layer.borderColor = UIColor.red.cgColor
return button
}
// Button Action
@objc func pressed(_ sender: UIButton) {
print("Pressed")
}
Swift: Ui Button create programmatically,
var button: UIButton = UIButton(type: .Custom)
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0)
button.addTarget(self, action: #selector(self.aMethod), forControlEvents: .TouchUpInside)
button.tag=2
button.setTitle("Hallo World", forState: .Normal)
view.addSubview(button)
func aMethod(sender: AnyObject) {
print("you clicked on button \(sender.tag)")
}
Using a closure
let button: UIButton = {
let button = UIButton(type: .system)
button.titleLabel?.font = UIFont.systemFont(ofSize: 20)
...
return button
}()
In iOS 12, Swift 4.2 & XCode 10.1
//For system type button
let button = UIButton(type: .system)
button.frame = CGRect(x: 100, y: 250, width: 100, height: 50)
// button.backgroundColor = .blue
button.setTitle("Button", for: .normal)
button.setTitleColor(.white, for: .normal)
button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 13.0)
button.titleLabel?.textAlignment = .center//Text alighment center
button.titleLabel?.numberOfLines = 0//To display multiple lines in UIButton
button.titleLabel?.lineBreakMode = .byWordWrapping//By word wrapping
button.tag = 1//To assign tag value
button.btnProperties()//Call UIButton properties from extension function
button.addTarget(self, action:#selector(self.buttonClicked), for: .touchUpInside)
self.view.addSubview(button)
//For custom type button (add image to your button)
let button2 = UIButton(type: .custom)
button2.frame = CGRect(x: 100, y: 400, width: 100, height: 50)
// button2.backgroundColor = .blue
button2.setImage(UIImage.init(named: "img.png"), for: .normal)
button2.tag = 2
button2.btnProperties()//Call UIButton properties from extension function
button2.addTarget(self, action:#selector(self.buttonClicked), for: .touchUpInside)
self.view.addSubview(button2)
@objc func buttonClicked(sender:UIButton) {
print("Button \(sender.tag) clicked")
}
//You can add UIButton properties like this also
extension UIButton {
func btnProperties() {
layer.cornerRadius = 10//Set button corner radious
clipsToBounds = true
backgroundColor = .blue//Set background colour
//titleLabel?.textAlignment = .center//add properties like this
}
}