gpt4 book ai didi

ios - layoutMarginsGuide 是不是有点过分了?

转载 作者:搜寻专家 更新时间:2023-11-01 05:55:09 24 4
gpt4 key购买 nike

当我尝试创建布局约束时,我读到了 NSLayoutAnchor 类。他们说:

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 属性的布局边距属性创建了 anchor 属性。

     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 确实为布局边距属性提供了 anchor 属性?如果有人知道什么,我将不胜感激。

最佳答案

这取决于您的设计要求。

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 的安全区域/边缘内,那么您将引用 otherViewlayoutMarginsGuide anchor 。
如果没有,那么只需 otherView 的 anchor 就足够了。

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)

输出:

On layoutMarginsGuide

观察:

  • 离开 self.view 的左/右边缘 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)

输出: On View Anchors alone

观察:

  • 立即从 self.view 的左/右边缘开始
  • 立即从 self.view 与状态栏重叠的上边缘开始
    • 如果有导航栏,它也会重叠
  • 这些是self.view的边界

最后还是要看你的要求了。
有时您应该使用它,有时您可以使用它,有时您只是不需要使用它。

PS:现在,无论您是基于 UIView 的 anchor 还是 layoutMarginsGuide anchor ,它都会自动响应设备旋转或任何其他布局更改。

关于ios - layoutMarginsGuide 是不是有点过分了?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50214263/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com