UIKit CheatSheet

Jul 25, 2022 1 min read
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
     }
 }
Great! Next, complete checkout for full access to ArturoFM.
Welcome back! You've successfully signed in.
You've successfully subscribed to ArturoFM.
Success! Your account is fully activated, you now have access to all content.
Success! Your billing info has been updated.
Your billing was not updated.