iOS 16 - Child controller not respecting safeAreaLayoutGuide

Originator:joao
Number:rdar://FB9997891 Date Originated:
Status:Open Resolved:
Product:UIKit Framework Product Version:iOS 16
Classification:Very High Reproducible:Yes
 
Tested on iOS 15.5 iPhone 13 Pro and it works, it doesn't on iPhone 16. 

Any clue what's going on?

Here's some sample code: 

class ViewController: UIViewController {

    let childController = ChildViewController()

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        
        view.backgroundColor = .yellow
        
        childController.willMove(toParent: self)
        view.addSubview(childController.view)

        childController.view.translatesAutoresizingMaskIntoConstraints = false
                NSLayoutConstraint.activate([
                    childController.view.topAnchor.constraint(equalTo: view.topAnchor),
                    childController.view.leadingAnchor.constraint(equalTo: view.leadingAnchor),
                    childController.view.trailingAnchor.constraint(equalTo: view.trailingAnchor),
                    childController.view.bottomAnchor.constraint(equalTo: view.bottomAnchor)
                ])
        
        childController.didMove(toParent: self)
    }
}

class ChildViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        
        let avoidSafeArea = UIView()
        avoidSafeArea.backgroundColor = .green
        
        view.addSubview(avoidSafeArea)
        
        avoidSafeArea.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate([
            avoidSafeArea.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
            avoidSafeArea.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor),
            avoidSafeArea.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor),
            avoidSafeArea.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor)
        ])
        
    }
}

Comments

You may be calling the wrong function

"Der Teilweise" wants to let you know: https://layer8.space/@teilweise/110185240128982744

Basically, you are calling willMove(:) but should be calling addChild(:).

By tempelmann at April 16, 2023, 5:35 p.m. (reply...)

Note

Clarifying that this testing happened on Xcode, using simulators.


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!