Reputation: 4209
I am working to migrate my app so I can use it on iPhoneX without scaling. Currently I layout a subview with the following code:
CGRect frame = self.view.bounds;
frame.size.height = 295;
frame.origin.y = 20;
This works fine until you add in the notch. I am not seeing a clear way to find out what the frame.origin.y should be set to, without just hard coding.. Which I would like to avoid.
What I would like to do is this - frame.origin.y = 20 + self.view.safeAreaInserts.top
But that doesn't do a thing.
Thanks!
Upvotes: 0
Views: 549
Reputation: 386058
Perhaps safeAreaInsets
is being changed after your code runs (for example if you're trying to perform layout in viewDidLoad
). You need to override safeAreaInsetsDidChange
to be notified when it changes.
Note that safeAreaInsetsDidChange
is a method of UIView
, not UIViewController
, so you'll need to create and use a subclass of UIView
to override the method. It would probably be sufficient to override it like this:
override func safeAreaInsetsDidChange() {
super.safeAreaInsetsDidChange()
setNeedsLayout()
}
And then you can set the subview's frame in layoutSubviews
.
Or you could just use constraints instead of setting the frame directly, and let auto layout handle it all for you.
Upvotes: 1