layoutMarginsGuide 是不是有点过分了?
Is layoutMarginsGuide seems to be kind of excessive?
当我尝试创建布局约束时,我读到了 NSLayoutAnchor class。他们说:
Note
UIView does not provide anchor properties for the layout margin
attributes. Instead, the layoutMarginsGuide property provides a
UILayoutGuide object that represents these margins. Use the guide’s
anchor properties to create your constraints
好的。但同时我在没有 UILayoutGuide 属性.
的情况下为布局边距属性创建了锚点属性
let inputsContainerView = UIView()
inputsContainerView.backgroundColor = UIColor.white
inputsContainerView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(inputsContainerView)
//need x,y,width,height constraints
inputsContainerView.centerXAnchor.constraint(equalTo: view.centerXAnchor, constant: 0).isActive = true
inputsContainerView.centerYAnchor.constraint(equalTo: view.centerYAnchor, constant: 0).isActive = true
inputsContainerView.widthAnchor.constraint(equalTo: view.widthAnchor, constant: -24).isActive = true
inputsContainerView.heightAnchor.constraint(equalToConstant: 150).isActive = true
那么为什么我们需要 UILayoutGuide 对象呢?事实证明 UIView 确实为布局边距属性提供了锚点属性?如果有人知道什么,我将不胜感激。
这取决于您的设计要求。
layoutMarginsGuide
documentation states:
A layout guide representing the view’s margins.
Use this layout guide’s anchors to create constraints with the view’s margin.
布局边距基本上只是视图的 safe area:
Safe areas help you place your views within the visible portion of the overall interface. UIKit-defined view controllers may position special views on top of your content. For example, a navigation controller displays a navigation bar on top of the underlying view controller’s content. Even when such views are partially transparent, they still occlude the content that is underneath them.
对于self.view
,整个界面的可见部分将不包括状态栏、导航栏、标签栏等占用的区域
对于正常 UIView
,默认填充为 8px。
所以基本上,如果您希望 someView
被限制在 otherView
的安全区域/边界内,那么您将参考 otherView
的 layoutMarginsGuide
锚点。
如果没有,那么 otherView
的锚点就足够了。
1。示例(layoutMarginsGuide
):
let textField = UILabel()
textField.text = "Using Layout Margins Guide Anchors"
textField.backgroundColor = UIColor.red
textField.translatesAutoresizingMaskIntoConstraints = false
self.view.addSubview(textField)
var constraints = [NSLayoutConstraint]()
//textfield's top edge = self.view's margin's top edge
constraints.append(textField.topAnchor.constraint(equalTo: self.view.layoutMarginsGuide.topAnchor))
//textfield's leading edge = self.view's margin's leading edge
constraints.append(textField.leadingAnchor.constraint(equalTo: self.view.layoutMarginsGuide.leadingAnchor))
//textfield's trailing edge = self.view's margin's trailing edge
constraints.append(textField.trailingAnchor.constraint(equalTo: self.view.layoutMarginsGuide.trailingAnchor))
//Apply the constraints
NSLayoutConstraint.activate(constraints)
输出:
观察:
- 从
self.view
的left/right边 留下20px
- 在状态栏正下方开始
- 如果您有导航栏,那么它会在该栏下方开始
- 这些是
self.view
的安全区域/布局边距
2。示例(没有 layoutMarginsGuide
):
let textField = UILabel()
textField.text = "Without Layout Margins Guide Anchors"
textField.backgroundColor = UIColor.red
textField.translatesAutoresizingMaskIntoConstraints = false
self.view.addSubview(textField)
var constraints = [NSLayoutConstraint]()
//textfield's top edge = self.view's top edge
constraints.append(textField.topAnchor.constraint(equalTo: self.view.topAnchor))
//textfield's leading edge = self.view's leading edge
constraints.append(textField.leadingAnchor.constraint(equalTo: self.view.leadingAnchor))
//textfield's trailing edge = self.view's trailing edge
constraints.append(textField.trailingAnchor.constraint(equalTo: self.view.trailingAnchor))
//Apply the constraints
NSLayoutConstraint.activate(constraints)
输出:
观察:
- 立即从
self.view
的 left/right 边开始
- 立即从
self.view
与状态栏重叠的上边缘开始
- 如果有导航栏,它也会重叠
- 这些是
self.view
的界限
最后就看你的要求了。
有时你应该使用它,有时你可以使用它,有时你根本不需要使用它。
PS:现在,无论您是基于 UIView
的锚点还是 layoutMarginsGuide
锚点,它都会自动响应设备旋转或任何其他布局更改。
当我尝试创建布局约束时,我读到了 NSLayoutAnchor class。他们说:
Note
UIView does not provide anchor properties for the layout margin attributes. Instead, the layoutMarginsGuide property provides a UILayoutGuide object that represents these margins. Use the guide’s anchor properties to create your constraints
好的。但同时我在没有 UILayoutGuide 属性.
的情况下为布局边距属性创建了锚点属性 let inputsContainerView = UIView()
inputsContainerView.backgroundColor = UIColor.white
inputsContainerView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(inputsContainerView)
//need x,y,width,height constraints
inputsContainerView.centerXAnchor.constraint(equalTo: view.centerXAnchor, constant: 0).isActive = true
inputsContainerView.centerYAnchor.constraint(equalTo: view.centerYAnchor, constant: 0).isActive = true
inputsContainerView.widthAnchor.constraint(equalTo: view.widthAnchor, constant: -24).isActive = true
inputsContainerView.heightAnchor.constraint(equalToConstant: 150).isActive = true
那么为什么我们需要 UILayoutGuide 对象呢?事实证明 UIView 确实为布局边距属性提供了锚点属性?如果有人知道什么,我将不胜感激。
这取决于您的设计要求。
layoutMarginsGuide
documentation states:
A layout guide representing the view’s margins. Use this layout guide’s anchors to create constraints with the view’s margin.
布局边距基本上只是视图的 safe area:
Safe areas help you place your views within the visible portion of the overall interface. UIKit-defined view controllers may position special views on top of your content. For example, a navigation controller displays a navigation bar on top of the underlying view controller’s content. Even when such views are partially transparent, they still occlude the content that is underneath them.
对于self.view
,整个界面的可见部分将不包括状态栏、导航栏、标签栏等占用的区域
对于正常 UIView
,默认填充为 8px。
所以基本上,如果您希望 someView
被限制在 otherView
的安全区域/边界内,那么您将参考 otherView
的 layoutMarginsGuide
锚点。
如果没有,那么 otherView
的锚点就足够了。
1。示例(layoutMarginsGuide
):
let textField = UILabel()
textField.text = "Using Layout Margins Guide Anchors"
textField.backgroundColor = UIColor.red
textField.translatesAutoresizingMaskIntoConstraints = false
self.view.addSubview(textField)
var constraints = [NSLayoutConstraint]()
//textfield's top edge = self.view's margin's top edge
constraints.append(textField.topAnchor.constraint(equalTo: self.view.layoutMarginsGuide.topAnchor))
//textfield's leading edge = self.view's margin's leading edge
constraints.append(textField.leadingAnchor.constraint(equalTo: self.view.layoutMarginsGuide.leadingAnchor))
//textfield's trailing edge = self.view's margin's trailing edge
constraints.append(textField.trailingAnchor.constraint(equalTo: self.view.layoutMarginsGuide.trailingAnchor))
//Apply the constraints
NSLayoutConstraint.activate(constraints)
输出:
观察:
- 从
self.view
的left/right边 留下20px
- 在状态栏正下方开始
- 如果您有导航栏,那么它会在该栏下方开始
- 这些是
self.view
的安全区域/布局边距
2。示例(没有 layoutMarginsGuide
):
let textField = UILabel()
textField.text = "Without Layout Margins Guide Anchors"
textField.backgroundColor = UIColor.red
textField.translatesAutoresizingMaskIntoConstraints = false
self.view.addSubview(textField)
var constraints = [NSLayoutConstraint]()
//textfield's top edge = self.view's top edge
constraints.append(textField.topAnchor.constraint(equalTo: self.view.topAnchor))
//textfield's leading edge = self.view's leading edge
constraints.append(textField.leadingAnchor.constraint(equalTo: self.view.leadingAnchor))
//textfield's trailing edge = self.view's trailing edge
constraints.append(textField.trailingAnchor.constraint(equalTo: self.view.trailingAnchor))
//Apply the constraints
NSLayoutConstraint.activate(constraints)
输出:
观察:
- 立即从
self.view
的 left/right 边开始 - 立即从
self.view
与状态栏重叠的上边缘开始- 如果有导航栏,它也会重叠
- 这些是
self.view
的界限
最后就看你的要求了。
有时你应该使用它,有时你可以使用它,有时你根本不需要使用它。
PS:现在,无论您是基于 UIView
的锚点还是 layoutMarginsGuide
锚点,它都会自动响应设备旋转或任何其他布局更改。