Deep understanding replaces simple memory
Top/Bottom Layout Guide
-
Initially (iOS 7), NSLayoutConstraint represents the constraint and is applied directly to the view
-
Later (iOS 9), a set of APIS for NSLayoutAnchor emerged because the API for directly adding constraints was too long
view1.topAnchor.constraint(equalTo: view2.centerYAnchor).isActive = true Copy the code
-
– Top/Bottom Layout Guide is also available to solve the problem of avoiding the Top navigation bar and status bar when Layout with root view of ViewController (because without these two guides, some magic numbers may appear in code)
-
TopLayoutGuide is of UILayoutSupportprotocol type
- This type is provided
length
For frame layout - Also provides
topAnchor
,bottomAnchor
,length
For Autolayout layouts
- This type is provided
Auto Layout Guide
IOS 11 and later was deprecated, replaced by the much better safeArea
LayoutMargins
A relative layout mechanism between views and subviews; Think of it as an area where subview layout is not recommended
- Every view has it
layoutMargins
andlayoutMarginsGuide
Two properties (corresponding to frame and Autolayout layouts) - This property tells subViews that I recommend that you do not cover up your layout
layoutMargins
Range of representation - So according to the idea above
- In the use of
layoutMarginsGuide
Or select IB layout relative to Margin when the superviewlayoutMargins
When changes occur, the position of the subview changes relative to each other - In addition to the linkage changes mentioned above, when
preservesSuperviewLayoutMargins
When the property is true (the default is false), linkage changes as well. What’s changing is that the view that sets the property to subview layout considers the layoutMargins set by View.superView. For example, the layoutMargins set by View.superView are pretty big, So that the subview may be laid out where the view.superview does not want it to be, and enabling this property will automatically adjust the subview position
- In the use of
- IOS 11 was introduced
directionalLayoutMargins
, it islayoutMargin
An updated version oflayoutMaring
There’s only left and right in the horizontal direction, butdirectionalLayoutMargins
Use leading and trailing to dynamically configure horizontal margins for languages with different reading directions
Positioning Content Within Layout Margins
safeArea
- Introduced in iOS 11, there is a safeArea for each UIView, which represents a layable safeArea that can’t be blocked by other controls
- The safeArea of a UIViewController view takes into account the Status bar, navigation bar, and TAB bar
- SafeArea is similar to layoutMargin, which is also reflected in autolayout and non-Autolayout
safeAreaLayoutGuide
Used for autolayoutsafeAreaInsets
For frame layout
- These two properties of safeArea cannot be modified
- But the view of UIViewController can be set
additionalSafeAreaInsets
To extend the safearea
- But the view of UIViewController can be set
Positioning Content Relative to the Safe Area
safeArea vs layoutMargin
As of now (iOS version 13 as of this writing)
-
Safearea was not created to replace layoutMargin. It can be seen from the fact that layoutMargin has not been abandoned that both mechanisms can work normally
-
LayoutMargin can be modified, safeArea cannot
-
Is that the only connection between insetsLayoutMarginsFromSafeArea when is true, and layoutMargin beyond the limits of safearea when layoutMargin will automatically adjust to the size of the safearea
-
Layout Guide, Margins, Insets and Safe Area demystified on iOS 10 & 11