Turn from: http://www.code4app.com/blog-822717-1471.html
For the iphone x adaptation, the main work to be done is to match the upper and lower unsafe areas.
In iOS development, there are two ways of Xib and code editing for layout. Both of these methods support absolute layout and AutoLayout two kinds.
Next, we'll explain how to automate the iphone x from Xib, code editing, absolute layout, and AutoLayout several layouts.
Xib layout
The absolute layout of the xib is not flexible, and if you want to change the view frame by a specific factor, you need to implement it through the property index. So this is only for Xib's AutoLayout to explain.
First XCode9 's Xib provides us with the safearealayout option, which does not support previous versions of IOS9.
Safearealayout
Then we need to constrain the view to the bottom or the top of the security zone to achieve the desired effect, but the constraint value is not fixed, you also need to balance the non-iphone x models.
Then you can find the solution from the following points:
First of all, our layout files are as follows:
The three identical labels are located at the bottom of the controller root view
Constraint file
First of all, if you are making a new page, then adding the constrain to margins property will help you when setting constraints, and the second label in the layout file is the layout using the margin property.
Constrain to margins
First of all, if you have a page that already exists, and you have set a constraint layout, you can find the corresponding constraint attribute, tick its relative to margin option, and point the constraint attribute to the Marigin.
Double-click it (╯>д<) ╯⁽˙³˙⁾
Constraint properties relative to margin portal
Tick It (╯>д<) ╯⁽˙³˙⁾
Relative to margin
Next we can see the effects of these three layouts
You can select the iphone x model to quickly view the results of the layout application by using View as: (certain model) in the bottom left corner of the layout view.
Layout effect
And the effect of the operation
Run effect
As you can see, the underlying constraints do not fit the unsecured zone at the bottom of the iphone x, and constrain to margins and relative to margin are perfectly suited to the iphone x.
Using Code layout
The layout of the code can still be AutoLayout, and it can be absolutely laid out by branching judgments.
AutoLayout
1. If you need to use the native API for AutoLayout layout, then you can use Nslayoutattributebottommargin as the bottom constraint enumeration value
1 2 |
Nslayoutconstraint *constraint = [Nslayoutconstraint constraintWithItem:self.leftLabel >attribute: Nslayoutattributebottommargin relatedby:nslayoutrelationequal ToItem:self.rightLabel >attribute: Nslayoutattributebottom multiplier:1 constant:0]; [Self.view Addconstraint:constraint]; |
2. If your AutoLayout is edited by masonry, then you only need to change the bottom constraint
In earlier versions of masonry
By: