UIButton Programmatically In Swift 5.

This tutorial for Adding UIButton inside UIView Programmatically with it's event in swift 5.




UIButton Code Swift 5:
override func viewDidLoad() {
    super.viewDidLoad()

    let button = UIButton(frame: CGRect(x: 0, y: 0, width: 200, height: 50))
    button.center = view.center
    button.backgroundColor = .black
    button.setTitle("iOSDevCenters Click", for: .normal)
    button.addTarget(self, action:#selector(self.buttonClicked), for: .touchUpInside)
    self.view.addSubview(button)

}

Event on UIButton:
@objc func buttonClicked() {
    print("Button Clicked")
}


Output:




Thanks.

UIButton Programmatically In Swift 5. UIButton Programmatically In Swift 5. Reviewed by KIRIT MODI on 21:14:00 Rating: 5

No comments:

Powered by Blogger.