iPhone X - safeAreaInsets & safeAreaLayoutGuide unset in UIVC viewWillAppear (iOS 11.0 & 11.1b1)

Originator:felipekellermann
Number:rdar://34858504 Date Originated:October 6 2017, 2:19 PM
Status:Closed Resolved:
Product:iOS + SDK Product Version:iOS 11.1b1 (15B5066e)
Classification:Suggestion Reproducible:Always
 
The `viewWillAppear` is often used to prepare some final interface elements before it appears to the user onscreen. The sizing is already known and the elements are are loaded and configured. While adapting our product to the new iPhone X screen using `safeAreaInsets` & `safeAreaLayoutGuide`, I noticed that in `viewWillAppear` the view `safeAreaInsets` & `safeAreaLayoutGuide` are not reflecting the iPhone X edges. When it gets to `viewDidAppear` everything is set and ready, but it can cause some minor user visible UI readjustments.

(lldb) po _cmd
"viewWillAppear:"
(lldb) p self.view.safeAreaInsets
(UIEdgeInsets) $11 = (top = 0, left = 0, bottom = 0, right = 0)
(lldb) po self.view.safeAreaLayoutGuide
<UILayoutGuide: 0x60c000fad660 - "UIViewSafeAreaLayoutGuide", layoutFrame = {{0, 0}, {812, 343}}, owningView = <UIView: 0x7fd2ab69c8f0; frame = (0 32; 812 343); autoresize = W+H; layer = <CALayer: 0x60c00222ae20>>>

(lldb) po _cmd
"viewDidAppear:"
(lldb) p self.view.safeAreaInsets
(UIEdgeInsets) $1 = (top = 0, left = 44, bottom = 0, right = 44)
(lldb) po self.view.safeAreaLayoutGuide
<UILayoutGuide: 0x60c0013ac160 - "UIViewSafeAreaLayoutGuide", layoutFrame = {{44, 0}, {724, 290}}, owningView = <UIView: 0x7f82176014d0; frame = (0 32; 812 290); autoresize = W+H; layer = <CALayer: 0x60c0002200e0>>>

Comments

I missed viewWillLayoutSubviews

The safeAreaInsets are only updated on a view once it has been added to the hierarchy. When -viewWillAppear: is called on a view controller, its view has not yet been added to the hierarchy. You should use a later lifecycle callback such as -viewWillLayoutSubviews when accessing the safeAreaInsets of the view.

By felipekellermann at Oct. 24, 2017, 3:19 p.m. (reply...)

Perhaps this is the cause for https://openradar.appspot.com/35116174


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!