UIKit CheatSheet

This page contains boiler plate to reuse on different views, along with some explanations. Feel free to leave a comment to add new elements.

View Controllers

How to present a sheet or push to another view

class ViewController: UIViewController {
    
     override func viewDidLoad() {
         super.viewDidLoad()
         let button = UIButton(frame: CGRect(x: view.frame.width / 2, y: view.frame.height / 2, width: 40, height: 40))
         button.backgroundColor = .red
         button.tintColor = .white
         button.setTitle("Go to another view", for: .normal)
         button.addTarget(self, action: #selector(change), for: .touchUpInside)
         view.addSubview(button)
     }
    @objc func change () {
        let vc = SecondViewController()
        
        // Present sheet
//         self.present(vc, animated: true, completion: nil)
        // Push navigation View Controller
        navigationController?.pushViewController(vc, animated: true)
     }
 }
 class SecondViewController: UIViewController {
     override func viewDidLoad() {
         super.viewDidLoad()
         view.backgroundColor = .yellow
     }
 }