Safearea
That is, the part that can display the content normally.
?
can be done byadditionalSafeAreaInsetsTo adjust the size of the Safearea.
?
After adjustment, the scope is as follows:
self.additionalSafeAreaInsets = UIEdgeInsets.init(top: 20, left: 30, bottom: 0, right: 10)
?
PasssafeAreaLayoutGuideCreate a constraint
bgView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor), bgView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor), bgView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor), bgView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor)
SubView's Safeareainsets
- Completely in-house,
safeAreaInsets for 0
- Part within Supview, but outside of Safearea,
safeAreaInsets for the size between
- Outside the Superview, the size of the parent view
safeAreaInsets does not extend out
?
view safeArea: UIEdgeInsets(top: 64.0, left: 30.0, bottom: 83.0, right: 10.0)bgview SafeArea: UIEdgeInsets(top: 0.0, left: 0.0, bottom: 0.0, right: 0.0)topview SafeArea: UIEdgeInsets(top: 64.0, left: 10.0, bottom: 0.0, right: 0.0)rightView SafeArea: UIEdgeInsets(top: 0.0, left: 0.0, bottom: 0.0, right: 10.0)
As shown above, Blue is completely within the safearea of Rootview, so it safeAreaInsets is 0.
TopView is within the bounds of Rootview, but outside of the safearea, so it reflects the rootview in bounds safeAreaInsets , outside the Safearea area.
The right side of the Rightview is beyond the screen, so the right side safeAreaInsets is the right side of the Rootview safeAreaInsets and will not continue to exceed.
Margin
That is, inner spacing?
UselayoutMarginsGuideTo create a constraint
bgView.topAnchor.constraint(equalTo: view.layoutMarginsGuide.topAnchor),bgView.bottomAnchor.constraint(equalTo: view.layoutMarginsGuide.bottomAnchor),bgView.leadingAnchor.constraint(equalTo: view.layoutMarginsGuide.leadingAnchor),bgView.trailingAnchor.constraint(equalTo: view.layoutMarginsGuide.trailingAnchor)
Insetslayoutmarginsfromsafearea Property
When the value of the margins is true, the "outside", the safe area was automatically modified to fall wit Hin the safe area boundary. The default value of the is true. Changing the value to False allows your margins to remain in their original locations, even when they is outside the safe Area.
That is, this property is guaranteed not to fall outside the Safearea area.
?
// 这里可以修改 layoutmargin topView.layoutMargins = UIEdgeInsets.init(top: 0, left: 0, bottom: 0, right: 0) [topView, rightView].forEach { (viewa) in let label = UILabel.init() label.translatesAutoresizingMaskIntoConstraints = false label.backgroundColor = UIColor.purple viewa?.addSubview(label) label.text = "tesx" let labelConstranits = [label.topAnchor.constraint(equalTo: viewa!.layoutMarginsGuide.topAnchor), label.leftAnchor.constraint(equalTo: viewa!.layoutMarginsGuide.leftAnchor)] NSLayoutConstraint.activate(labelConstranits) // 如果这个是false,那么topview中的label,可以出现在safeArea区域之外 topView.layoutMargins维持为0 // 如果这个是true,那么topview的lable一定在safeArea之内。topView.layoutMargins也会变化 viewa!.insetsLayoutMarginsFromSafeArea = false }
As shown, the Lable falls outside the Safearea, and if insetsLayoutMarginsFromSafeArea set to True, the result is as follows:
?
It is important to note that when creating a constraint, it must be used layoutMarginsGuide , otherwise, even if insetsLayoutMarginsFromSafeArea set to True, it will fall outside of Safearea.
Preservessuperviewlayoutmargins
When the value of the "is" true, the Superview ' s margins is also considered when laying out content. This margin affects layouts where the distance between the edge of a view and its superview are smaller than the correspond ing margin. For example, you might has a content view whose frame precisely matches the bounds of its superview. When any of the Superview's margins is inside the area represented by the content view and its own margins, UIKit adjusts The content view's layout to respect the Superview ' s margins. The amount of the adjustment is the smallest amount needed to ensure that content is also inside the Superview ' s margins.
The documentation is a bit of a blur, let's look at an example.
- The Blue view has a purple view, and the purple View adds a white view
- The Blue view is
layoutMargins(top: 100, left: 50, bottom: 100, right: 40)
- Purple view is constrained to the top of the bottom difference of 10, spread around
preservesSuperviewLayoutMarginsTrue for Purple view
- The white view is constrained to the upper and lower left and the purple view
layoutMarginsGuide alignment
- The Purple view is
layoutMargins(top: 0, left: 0, bottom: 0, right: 0)
If preservesSuperviewLayoutMargins false, the white view and the purple view are exactly the same size. Because this property is true, the purple view is in the layout, taking into account the parent view layoutMargins , so the white view size is reduced.
Viewrespectssystemminimumlayoutmargins
The VC of the system has a default minimum inner spacing, which is read-only and not writable, called systemMinimumLayoutMargins .
Even if we set the Rootview directionalLayoutMargins , if it is too small, the system will fall back to the system default minimum spacing.
?
As shown above, VC rootview spacing is set to zero, in accordance with the layoutMarginsGuide creation of constraints, left and right there are still internal spacing. is because of this VC systemMinimumLayoutMargins (top: 0.0, leading: 16.0, bottom: 0.0, trailing: 16.0) .
If viewRespectsSystemMinimumLayoutMargins set to False, it can be extended to the edge.
?
Reference
- Uikit:apps for every Size and Shape
- Preserves Superview Layout Margins
- Imanoupetit/margins-and-safe-area
- Demo
Uikit:apps for every Size and Shape