- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
当我尝试创建布局约束时,我读到了 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
的安全区域/边缘内,那么您将引用 otherView
的layoutMarginsGuide
anchor 。
如果没有,那么只需 otherView
的 anchor 就足够了。
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
的左/右边缘 20pxself.view
的安全区域/布局边距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
的左/右边缘开始self.view
与状态栏重叠的上边缘开始
self.view
的边界最后还是要看你的要求了。
有时您应该使用它,有时您可以使用它,有时您只是不需要使用它。
PS:现在,无论您是基于 UIView
的 anchor 还是 layoutMarginsGuide
anchor ,它都会自动响应设备旋转或任何其他布局更改。
关于ios - layoutMarginsGuide 是不是有点过分了?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50214263/
当我尝试创建布局约束时,我读到了 NSLayoutAnchor 类。他们说: Note UIView does not provide anchor properties for the layout
如何重写视觉格式 addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("|-[label]-|", options: .Alig
我正在尝试为包含 textView 的 TableView 创建自定义节标题。我根据教程创建了一个简单的应用程序。这将创建一个工作示例。但是,当我将代码移动到生产应用程序时,它的布局不正确。简单的教程
我是一名优秀的程序员,十分优秀!