什么是“限制保证金"?在 Xcode 6 的故事板中 [英] What is "Constrain to margin" in Storyboard in Xcode 6

查看:31
本文介绍了什么是“限制保证金"?在 Xcode 6 的故事板中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用自动布局和约束,发现 Xcode 6 中有一个 Constrain to margins 选项,该选项在 Xcode 5 中不存在,默认情况下已选中.

I am Working with autolayout and constraints and found there is a Constrain to margins option in Xcode 6 which was not present in Xcode 5 and is checked by default.

我创建了一个测试项目,然后我在 ViewController 上添加了一个 UITableView,框架设置为与视图相同的大小并添加了约束

I created a test project then I added a UITableView on a ViewController with the frame set to the same size as view and added constraints

Xcode 6即使 tableview 与视图具有相同的框架,您也可以在此处看到 Xcode 建议添加 -16 作为约束,而 Xcode 5 建议添加间距 0.

Xcode 6 You can see here even though tableview has the same frame as view Xcode suggests to add -16 as constraint whereas Xcode 5 would suggest adding spacing 0.

现在,当您取消选中Constrain to margin"选项时,它的行为与 Xcode 5 相同,并建议添加 0 作为约束

Now when you uncheck "Constrain to margin" option it behaves same as Xcode 5 and would suggest adding 0 as constraint

另外,我发现一旦我添加了 Constrain to margin check 的约束,我就无法再在 Xcode 5 中打开故事板文件,所以它绝对是 Xcode 6 中的新东西

Also, I found that once I add constraint with Constrain to margin checked, I am no longer able to open the storyboard file in Xcode 5 so it's definitely something new in Xcode 6

希望我能够正确解释我的问题.我想了解Constrain to margin"实际上做了什么,以及何时应该和不应该使用它.如果事情非常简单和明显,我深表歉意.

Hopefully, I am able to explain my question properly. I would like to understand what "Constrain to margin" actually does and when I should and should not use it. I do apologize if it's something very simple and obvious.

编辑

我在 讨论,不知道是否与此有关.

I found something about layout margins in discussion here , I wonder if it's related to this.

推荐答案

我完全不明白为什么人们抱怨Margins 会导致 iOS 8 之前的任何东西彻底崩溃."

I don't understand at all why people are complaining that "Margins would cause an outright crash on anything prior to iOS 8."

在 xib 文件或故事板中设置相对于边距的约束不会使您的应用在 iOS7 上崩溃,并且它不会在您的 iOS7 设备上产生 UI 差异两者都不是,只要您不触及代码中的 UIView.layoutMarginsUIView.preservesSuperviewLayoutMargins 属性.

Setting your constraints relative to margin in a xib file or storyboard DOES NOT make your app crash on iOS7, and it DOES NOT make a UI difference on your iOS7 device neither, as long as you don't touch the UIView.layoutMargins and UIView.preservesSuperviewLayoutMargins properties in your code.

iOS8 中的边距是什么

布局边距表示 UIView内部 周围的填充,布局系统可以在布局子视图时使用它 - 以确保在视图和子视图.在这方面,它非常类似于 CSS 中与块相关的 padding 属性.

What is Margins in iOS8

Layout margins represent padding around the interior of a UIView that the layout system can use when laying out subviews - to ensure that a gap is left between the edge of a view and a subview. In this respect it is very much like the padding property associated with blocks in CSS.

默认情况下,UIView 每边的布局边距为 8 点,并且在 Interface Builder 中无法更改.但是,通过在代码中设置 UIView.layoutMargins 属性(仅在 iOS8 上可用),您可以调整这些值.

By default, a UIView has layout margins of 8 points on each side, and this can not be changed in Interface Builder. However, by setting the UIView.layoutMargins property in the code, which is only available on iOS8, you are able to adjust these values.

您可以使用 Editor > 来让 IB 显示边距.画布 >显示布局矩形:

边距可用于帮助布局视图和子视图.默认情况下,每个 UIView 都带有边距,但它们只会在您设置与边距相关的约束时影响视图位置.

Margins can be used to help layout your views and subviews. Every UIView come with margins by default, but they only affect view placement when you set up a constraint that is related to a margin.

在 Interface Builder 中使用边距的唯一方法是在配置约束时选中相对边距选项.这就是你如何指导你的约束在布局我的视图时使用边距而不是边缘.

The only way to use margins in Interface Builder is to check the Relative to margin option while configuring your constraints. This is how you direct your constraint to Use margins instead of edges when laying out my view.

让我们看一下在视图与其子视图之间设置前导约束的四种不同方法.对于每个约束,我们审查描述的第一个关联将是子视图的领先,而第二个将是超级视图的领先.您要密切关注的是每个约束端的Relative to margin 选项的勾选和取消勾选状态,因为它定义了约束是绑定到边距还是绑定到视图的边缘.

Let's take a look at four different ways of setting up a leading constraint between a view and its subview. For each constraint we review the first association described will be the subview's leading, and the second will be superview's leading. What you want to pay close attention to is the check and uncheck status of the Relative to margin option of each constraint end, because that defines whether the constraint is tied to the margin or the edge of the view.

  1. 第一项(取消选中),第二项(选中):在这种情况下,我们声明子视图的左边缘应与超级视图的左边缘对齐(如图所示).

  1. 第一项(取消选中),第二项(取消选中):均使用边距,不使用边距.在这种情况下,我们声明子视图的左边缘应该与超级视图的左边缘对齐.
  1. First item(uncheck), second item(uncheck): Both using edge, not margin. In this case, we're declaring that subview's left edge should align to superview's left edge.

  1. 第一项(选中),第二项(取消选中):在这种情况下,我们声明子视图的左边距应该与超级视图的左边对齐.这种布局实际上是让子视图与父视图重叠.

  1. 第一项(检查),第二项(检查).这实际上与情况 2 具有相同的效果,因为 subview 和 superview 具有相同的默认边距.我们声明子视图的左边距应该与超级视图的左边距对齐.

如果您决定使用边距,此新功能 (iOS8) 只会影响 UI 开发.

This new feature (iOS8) only impacts UI development if you decide to use margins.

通过使用边距,您可以通过更改单个属性的值来调整与共享超级视图共享公共关系的多个子视图的位置.这明显优于使用固定值设置所有相关约束,因为如果您需要更新所有间距,而不是一个一个更改每个值,您可以通过使用一行更新超级视图的边距来同时修改所有相关位置像这样的代码:

By using margins you can adjust the placement of multiple subviews that share a common relation to a shared superview by changing the value of a single property. This is a clear win over setting all associated constraints with fixed values, because if you need to update all the spacing, instead of changing each value one by one, you can simultaneously modify all relevant placement by updating the superview's margin with a single line of code like this one:

self.rootView.layoutMargins = UIEdgeInsetsMake(0, 50, 0, 0);

为了说明这个好处,在下面的例子中,所有子视图的左边缘都与它们的父视图的左边距对齐.因此,改变superview的左边距会同时影响所有subview.

To illustrate this benefit, in the following case all subviews' left edges are aligned to their superview's left margin. Thus, changing superview's left margin will affect all subviews at the same time.

这篇关于什么是“限制保证金"?在 Xcode 6 的故事板中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆