Since I don't use storyboards to create my views, I was wondering if there's the "Use Safe Area Guides" option programmatically or something like that.
I've tried to anchor my views to
view.safeAreaLayoutGuide
but they keep overlapping the top notch in the iPhone X simulator.
For those of you who use SnapKit, just like me, the solution is anchoring your constraints to
view.safeAreaLayoutGuide
like so:I'm using this instead of add leading and trailing margin constraints to the layoutMarginsGuide:
Please also check the option for lower version of ios 11 from Krunal's answer.
Use
UIWindow
orUIView
'ssafeAreaInsets
.bottom
.top
.left
.right
Here is sample code (Ref from: Safe Area Layout Guide):
If you create your constraints in code use the safeAreaLayoutGuide property of UIView to get the relevant layout anchors. Let’s recreate the above Interface Builder example in code to see how it looks:
Assuming we have the green view as a property in our view controller:
We might have a function to set up the views and constraints called from viewDidLoad:
Create the leading and trailing margin constraints as always using the layoutMarginsGuide of the root view:
Now, unless you are targeting iOS 11 and later, you will need to wrap the safe area layout guide constraints with #available and fall back to top and bottom layout guides for earlier iOS versions:
Result:
Here is Apple Developer Official Documentation for Safe Area Layout Guide
Safe Area is required to handle user interface design for iPhone-X. Here is basic guideline for How to design user interface for iPhone-X using Safe Area Layout
Use constraints with visual format and you get respect for the safe area for free.
SafeAreaLayoutGuide
isUIView
property,Use
safeAreaLayoutGuide
for avoid our objects clipping/overlapping from rounded corners, navigation bars, tab bars, toolbars, and other ancestor views.We can create
safeAreaLayoutGuide
object & set object constraints respectively.Constraints for Portrait + Landscape is -
UILayoutGuide
safeAreaLayoutGuide