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
}
}