import UIKit
class ViewController: UIViewController {
let welcomeLabel = UILabel()
let continueButton = UIButton(type: .system)
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white
welcomeLabel.text = "Welcome to App"
welcomeLabel.textAlignment = .center
welcomeLabel.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(welcomeLabel)
continueButton.setTitle("Continue", for: .normal)
continueButton.translatesAutoresizingMaskIntoConstraints = false
continueButton.addTarget(self, action: #selector(continueTapped), for: .touchUpInside)
view.addSubview(continueButton)
NSLayoutConstraint.activate([
welcomeLabel.centerXAnchor.constraint(equalTo: view.centerXAnchor),
welcomeLabel.centerYAnchor.constraint(equalTo: view.centerYAnchor),
continueButton.topAnchor.constraint(equalTo: welcomeLabel.bottomAnchor, constant: 20),
continueButton.centerXAnchor.constraint(equalTo: view.centerXAnchor)
])
}
@objc func continueTapped() {
print("Continue tapped")
}
}We create a UILabel and a UIButton programmatically. The label is centered in the view. The button is placed 20 points below the label and centered horizontally. We add a target action to the button that prints a message when tapped. Auto Layout constraints position the elements properly.