I need to adjust my views for iPhone X but I can’t figure out when the safeAreaInsets are initialized. According to the documentation,
If the view is not currently installed in a view hierarchy, or is not yet visible onscreen, the edge insets in this property are 0.
I would think that when viewDidLoad is called, that the values would be set, but that is not the case. I can get values when viewDidLayoutSubviews is called, but that seems to be too late and doesn’t return the correct values anyway.
Can anyone explain how to use the safeAreaInsets property to me?
The safe area of a view reflects the area not covered by navigation bars, tab bars, toolbars, and other ancestors that obscure a view controller's view. (In tvOS, the safe area reflects the area not covered by the screen's bezel.)
UIEdgeInsets encapsulates four different CGFloat values for the inset values of the top, left, bottom, and right (respectively) individually. Positive inset values shrink the content area, while negative inset values will effectively increase it.
They are initialized before view is layouted (hence within view hierarchy). Best place to access them and act accordingly is within viewWillLayoutSubviews method. As you have already mentioned viewDidLayoutSubviews is little too late, but willLayoutSubviews works just fine. Happy coding!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With