iOS

View Hierarchy

// // ViewController.swift // ViewHieararcy // // Created by JOYNAL ABEDIN on 30/7/22. // import UIKit class ViewController: UIViewController { // override func loadView() { // loadV...

J
Joynal Abedin
4
View Hierarchy
//
//  ViewController.swift
//  ViewHieararcy
//
//  Created by JOYNAL ABEDIN on 30/7/22.
//

import UIKit

class ViewController: UIViewController {

    //    override func loadView() {
    
    //    loadView() exists to create the view that is managed by the view controller. It feels a little…unusual to use if you are used to using Storyboards for your projects for this reason.
    //    If you use Interface Builder to create your views and initialize the view controller, you must not override this (loadView()) method.
    //        // This is where subclasses should create their custom view hierarchy if they aren't using a nib. Should never be called directly.
    //        print(\"✅loadView called..\")
    //    }

        override func viewDidLoad() {
            super.viewDidLoad()
            // Called after the view has been loaded. For view controllers created in code, this is after -loadView. For view controllers unarchived from a nib, this is after the view is set
            // Do any additional setup after loading the view.
            print(\"✅viewDidLoad called\")
            
        }
        
        override func viewWillAppear(_ animated: Bool) {
            // Called when the view is about to made visible. Default does nothing
            print(\"✅viewWillAppear\")
        }
        
        override func viewDidAppear(_ animated: Bool) {
            // Called when the view has been fully transitioned onto the screen. Default does nothing
            print(\"✅viewDidAppear\")
        }
        
        override func viewWillDisappear(_ animated: Bool) {
            // Called when the view is dismissed, covered or otherwise hidden. Default does nothing
            print(\"✅viewWillDisappear\")
        }
        
        override func viewDidDisappear(_ animated: Bool) {
            // Called after the view was dismissed, covered or otherwise hidden. Default does nothing
            print(\"✅viewDidDisappear\")
        }
        
        override func viewWillLayoutSubviews() {
            // Called just before the view controller's view's layoutSubviews method is invoked. Subclasses can implement as necessary. The default is a no-op.
            print(\"✅viewWillLayoutSubviews\")
        }
        
        override func viewDidLayoutSubviews() {
            // Called just after the view controller's view's layoutSubviews method is invoked. Subclasses can implement as necessary. The default is a no-op.
            print(\"✅viewDidLayoutSubviews\")
        }
        
        override func loadViewIfNeeded() {
            // Loads the view controller's view if it has not already been set.
            print(\"✅loadViewIfNeeded\")
        }
}
Output: ✅viewDidLoad called ✅viewWillAppear ✅viewWillLayoutSubviews ✅viewDidLayoutSubviews ✅viewDidAppear   Reference: https://medium.com/swlh/write-clean-code-by-overriding-loadview-ac4f172163d0
J

Written by Joynal Abedin

Passionate about technology, code, and sharing knowledge.

0 Comments

Leave a Comment