JScarry
JScarry

Reputation: 1507

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?

Upvotes: 6

Views: 1546

Answers (1)

Raimundas Sakalauskas
Raimundas Sakalauskas

Reputation: 2294

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!

Upvotes: 3

Related Questions