I'm using safearealayout guides, programmatically (i.e.) without storyboards. I want to access the safeAreaLayoutGuides
, layoutFrame
size within my app but don't know when to call it. If you call on viewDidLoad
, the size returned is the same as the full view.frame size
. When does the app catch up and readjust? My full app has 5 screens within a menubarcontroller
. The first screen loads with content constrained to view.topAnchor
when I've activated view.safeAreaLayoutGuide.topAnchor
. If you switch to another tab/screen, that screen is okay and when you return to the first the layout has corrected itself. In a basic example, I added a button which prints the frame sizes after the view has loaded and it returns the correct size. I don't want my user to have to press a button to load the view correctly.
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
print(view.frame) // Returns (0.0, 0.0, 375.0, 812.0)
print(view.safeAreaLayoutGuide.layoutFrame) // Returns (0.0, 0.0, 375.0, 812.0)
}
@IBAction func button(_ sender: Any) {
print(view.frame) // Returns (0.0, 0.0, 375.0, 812.0)
print(view.safeAreaLayoutGuide.layoutFrame) // Returns (0.0, 44.0, 375.0, 734.0)
}
}
Thanks to Milan. I have experimented and found the following:
`
Results from console