I'm trying to implement additionalSafeAreaInsets, similar to the example from the Apples documentation, but the child view controller is not respecting the additional insets:
The expectation is that the left column view (leftView) is juxtaposed to a child view controller (childVC) within a root view controller (MyVC), but the child view controller simply takes up the entire main view.
Code Block swift import UIKit import PlaygroundSupport class MyVC: UIViewController { var leftView: UIView! let secondVC = SecondVC() override func viewDidLoad() { super.viewDidLoad() leftView = UIView(frame: CGRect(origin: .zero, size: .init(width: 50, height: self.view.bounds.size.height))) leftView.backgroundColor = .blue self.view.addSubview(leftView) } override func viewDidAppear(_ animated: Bool) { super.viewDidAppear(true) var newSafeArea = UIEdgeInsets() if let sideViewWidth = leftView?.bounds.size.width { newSafeArea.left += sideViewWidth } print(newSafeArea) addChild(secondVC) secondVC.view.frame = self.view.safeAreaLayoutGuide.layoutFrame self.view.addSubview(secondVC.view) secondVC.didMove(toParent: self) secondVC.additionalSafeAreaInsets = newSafeArea } } class SecondVC: UIViewController { override func viewDidLoad() { super.viewDidLoad() self.view.backgroundColor = .yellow } } PlaygroundPage.current.needsIndefiniteExecution = true PlaygroundPage.current.liveView = MyVC()
The expectation is that the left column view (leftView) is juxtaposed to a child view controller (childVC) within a root view controller (MyVC), but the child view controller simply takes up the entire main view.
So, this code is tested in the Playground of Version 12.2 (12B45b), but the behavior would not be affected by minor versions.
You can experiment by modifying newSafeArea or commenting out line 24.
The code in the Apples doc uses childViewControllers, but it is renamed to children in Swift. Seems the code is a little bit outdated. But it is not a big issue here.
The property children makes sense only in a container view controller.
That means the example code is intended to be used in some sort of container view controllers, such as UINavigationController or UITabBarController,
to affect the layout of the content view controller using constraints.
Code Block import UIKit import PlaygroundSupport class MyVC: UINavigationController { var leftView: UIView! override func viewDidLoad() { super.viewDidLoad() leftView = UIView(frame: CGRect(origin: .zero, size: .init(width: 50, height: self.view.bounds.size.height))) leftView.backgroundColor = UIColor.blue.withAlphaComponent(0.5) self.view.addSubview(leftView) } override func viewDidAppear(_ animated: Bool) { super.viewDidAppear(true) var newSafeArea = UIEdgeInsets() if let sideViewWidth = leftView?.bounds.size.width { newSafeArea.left += sideViewWidth } print(newSafeArea) children[0].additionalSafeAreaInsets = newSafeArea } } class SecondVC: UIViewController { override func viewDidLoad() { super.viewDidLoad() self.view.backgroundColor = .yellow let subview = UIView(frame: CGRect(origin: .zero, size: self.view.frame.size)) subview.translatesAutoresizingMaskIntoConstraints = false self.view.addSubview(subview) subview.backgroundColor = .green NSLayoutConstraint.activate([ subview.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor), subview.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor), subview.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor), subview.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor), ]) } } PlaygroundPage.current.needsIndefiniteExecution = true let theVC = MyVC(rootViewController: SecondVC()) PlaygroundPage.current.liveView = theVC
You can experiment by modifying newSafeArea or commenting out line 24.
The code in the Apples doc uses childViewControllers, but it is renamed to children in Swift. Seems the code is a little bit outdated. But it is not a big issue here.
The property children makes sense only in a container view controller.
That means the example code is intended to be used in some sort of container view controllers, such as UINavigationController or UITabBarController,
to affect the layout of the content view controller using constraints.