Do child view controllers inherit the frame of their parents?

Below is my code. I have the ViewController, which takes up entire screen (set background color, makes sure status bar is visible, etc.

It then calls up the MainController, which is set to be only in the safeAreaLayout frame. It has a button that brings up a third view controller when clicked.

Everything works, the ViewController covers the entire screen, the MainController rests within the safeAreaLayouts of the iPhone X, and the third view controller comes up the same size and position as the MainController.

It's that last part I want to make sure of, that that is the way it is supposed to come up. Can I count on that? Or must I set its frame myself to be sure?

ViewController

class ViewController: UIViewController {

    var mainController = MainController()
        
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()

        self.addChild(mainController)
        self.view.addSubview(mainController.view)
        setConstraints(vc: mainController, pc: view)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = bgColor
    }

    override var prefersStatusBarHidden: Bool {
           return false
       }

    override var preferredStatusBarStyle: UIStatusBarStyle {
       return .darkContent
    }
}

func setConstraints (vc: UIViewController, pc: UIView) {
    vc.view.translatesAutoresizingMaskIntoConstraints = false
    var constraints = [NSLayoutConstraint]()
    constraints.append(vc.view.leadingAnchor.constraint(equalTo: pc.safeAreaLayoutGuide.leadingAnchor))
    constraints.append(vc.view.trailingAnchor.constraint(equalTo: pc.safeAreaLayoutGuide.trailingAnchor))
    constraints.append(vc.view.bottomAnchor.constraint(equalTo: pc.safeAreaLayoutGuide.bottomAnchor))
    constraints.append(vc.view.topAnchor.constraint(equalTo: pc.safeAreaLayoutGuide.topAnchor))
     NSLayoutConstraint.activate(constraints)
}

MainController

class MainController: UIViewController {
    
    private lazy var countController = CountController()
    var invButton   : MyButton!

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .black

        ...button code ....
    }

    override var prefersStatusBarHidden: Bool {
        return false
    }
    
    @objc func buttonAction(sender: UIButton!) {
        guard let theButton = sender as? MyButton else { return}
        self.addChild(countController)
        self.view.addSubview(countController.view)
    }
}

ThirdViewController


class CountController : UIViewController {

    var backButton : MyButton!

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = .gray
    }
}
Answered by Claude31 in 696177022

and the third view controller comes up the same size and position as the MainController.

If I understand your question, this has nothing to do with inheritance.

It just depends on how you defined the transition: full screen / automatic…

Accepted Answer

and the third view controller comes up the same size and position as the MainController.

If I understand your question, this has nothing to do with inheritance.

It just depends on how you defined the transition: full screen / automatic…

How do you transition from one controller to the next ? With a segue ? You can define in IB (or in code) the transition mode:

Set it to full Cover, both for parent and child.

Do child view controllers inherit the frame of their parents?
 
 
Q