问题描述
我正在使用自动布局和约束,发现 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 添加约束到边距检查,我不再能够在 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
希望我能够正确解释我的问题.我想了解限制边距"的实际作用以及何时应该和不应该使用它.如果这是非常简单明了的事情,我深表歉意.
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.layoutMargins
和 UIView.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
andUIView.preservesSuperviewLayoutMargins
properties in your code.
什么是 iOS8 中的边距
布局边距表示 UIView
的 interior 周围的填充,布局系统在布置子视图时可以使用该填充 - 以确保在视图和子视图.在这方面,它非常类似于 CSS 中与块相关的填充属性.
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 中使用边距的唯一方法是在配置约束时检查 Relative to margin 选项.这就是你如何引导你的约束来在布局我的视图时使用边距而不是边缘.
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.
- 第一项(取消选中),第二项(选中):在这种情况下,我们声明子视图的左边缘应与父视图的左边距对齐(如图所示).
- 第一项(取消选中),第二项(取消选中):都使用边缘,不是边距.在这种情况下,我们声明子视图的左边缘应与父视图的左边缘对齐.
- 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.
- 第一项(选中),第二项(取消选中):在这种情况下,我们声明子视图的左边距应该与父视图的左边缘对齐.这种布局实际上让 subview 和 superview 重叠.
- 第一项(检查),第二项(检查).这实际上与案例 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 的情节提要中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!