uikit programatically rectangular view with a button and a label in swift

main.swift
import UIKit

class RectangleViewController: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // create a rectangular view
        let rectangleView = UIView(frame: CGRect(x: 50, y: 50, width: 200, height: 100))
        rectangleView.backgroundColor = UIColor.white
        rectangleView.layer.cornerRadius = 15
        rectangleView.layer.borderWidth = 1
        rectangleView.layer.borderColor = UIColor.black.cgColor
        view.addSubview(rectangleView)
        
        // create a label
        let label = UILabel(frame: CGRect(x: 10, y: 10, width: 180, height: 30))
        label.text = "Hello, World!"
        label.textAlignment = .center
        rectangleView.addSubview(label)
        
        // create a button
        let button = UIButton(type: .system)
        button.frame = CGRect(x: 50, y: 50, width: 100, height: 30)
        button.setTitle("Button", for: .normal)
        button.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside)
        rectangleView.addSubview(button)
    }
    
    @objc func buttonTapped(sender: UIButton) {
        print("Button tapped!")
    }
    
}
1171 chars
35 lines

gistlibby LogSnag