Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When can you call UIEdgeInsets safeAreaInsets?

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?

like image 430
JScarry Avatar asked Sep 26 '17 17:09

JScarry


People also ask

What is SafeAreaInsets swift?

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.)

What is UIEdgeInsets?

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.


1 Answers

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!

like image 128
Raimundas Sakalauskas Avatar answered Oct 22 '22 09:10

Raimundas Sakalauskas