ios 什么是 Xcode 6 Storyboard 中的“Constrain to margin”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25807545/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is "Constrain to margin" in Storyboard in Xcode 6
提问by Bhumit Mehta
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.
我正在使用自动布局和约束,发现Constrain to margins
Xcode 6 中有一个选项,该选项在 Xcode 5 中不存在,默认情况下已选中。
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
我创建了一个测试项目,然后UITableView
在 ViewController 上添加了一个,框架设置为与视图相同的大小并添加了约束
Xcode 6You 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.
Xcode 6您可以在此处看到,即使 tableview 与视图具有相同的框架,Xcode 建议添加 -16 作为约束,而 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”选项时,它的行为与 Xcode 5 相同,并建议添加 0 作为约束
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 check 的约束,我就无法再在 Xcode 5 中打开故事板文件,所以它绝对是 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.
希望我能够正确解释我的问题。我想了解“Constrain to margin”实际上做了什么,以及什么时候应该和不应该使用它。如果这是非常简单和明显的事情,我深表歉意。
EDIT
编辑
I found something about layout margins in discussion here, I wonder if it's related to this.
回答by Scott Zhu
I don't understand at all why people are complaining that "Margins would cause an outright crash on anything prior to iOS 8."
我完全不明白为什么人们抱怨“在 iOS 8 之前的任何东西上,边距都会导致彻底崩溃。”
Setting your constraints relative to margin in a xib file or storyboard DOES NOTmake your app crash on iOS7, and it DOES NOTmake a UI difference on your iOS7 device neither, as long as you don't touch the
UIView.layoutMargins
andUIView.preservesSuperviewLayoutMargins
properties in your code.
在 xib 文件或故事板中设置相对于边距的约束不会使您的应用程序在 iOS7 上崩溃,也不会在您的 iOS7 设备上产生 UI 差异,只要您不触及代码中的
UIView.layoutMargins
和UIView.preservesSuperviewLayoutMargins
属性。
What is Margins in iOS8
什么是 iOS8 中的边距
Layout margins represent padding around the interiorof 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
该布局当子视图的布局系统可以使用-确保的间隙的视图和一个子视图的边缘之间留下。在这方面,它非常类似于 CSS 中与块相关的 padding 属性。
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.
默认情况下, aUIView
每边的布局边距为 8 点,并且不能在Interface Builder 中更改。但是,通过UIView.layoutMargins
在代码中设置该属性(仅在 iOS8 上可用),您可以调整这些值。
You can get IB to display the margins with Editor > Canvas > Show Layout Rectangles:
您可以让 IB 使用Editor > Canvas > Show Layout Rectangles来显示边距:
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.
边距可用于帮助布局视图和子视图。UIView
默认情况下,每个都带有边距,但它们只会在您设置与边距相关的约束时影响视图放置。
How to use Margins
如何使用边距
The only way to use margins in Interface Builder is to check the Relative to marginoption while configuring your constraints. This is how you direct your constraint to Use margins instead of edges when laying out my view.
在 Interface Builder 中使用边距的唯一方法是在配置约束时检查相对边距选项。这就是在布局我的视图时将约束引导到使用边距而不是边缘的方式。
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 marginoption 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(check): In this case, we're declaring that subview's left edge should align to superview's left margin(as shown in this image).
- 第一项(取消选中),第二项(选中):在这种情况下,我们声明子视图的左边缘应与超级视图的左边缘对齐(如图所示)。
- 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.
- 第一项(取消选中),第二项(取消选中):都使用 edge,而不是 margin。在这种情况下,我们声明子视图的左边缘应该与超级视图的左边缘对齐。
- First item(check), second item(uncheck): In this case, we're declaring that subview's left margin should align to superview's left edge. This kind of layout actually makes the subview overlap the superview.
- 第一项(选中),第二项(取消选中):在这种情况下,我们声明子视图的左边距应该与超级视图的左边距对齐。这种布局实际上是让子视图与父视图重叠。
- First item(check), second item(check). This actually has a same effect as case 2, since both subview and superview has a same default margin. We're declaring that subview's left margin should align to superview's left margin.
- 第一项(检查),第二项(检查)。这实际上与情况 2 具有相同的效果,因为 subview 和 superview 具有相同的默认边距。我们声明子视图的左边距应该与超级视图的左边距对齐。
What is good about Margins
保证金有什么好处
This new feature (iOS8) only impacts UI development if you decide to use margins.
如果您决定使用边距,此新功能 (iOS8) 只会影响 UI 开发。
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);
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.
为了说明这个好处,在下面的例子中,所有子视图的左边缘都与它们的父视图的左边缘对齐。因此,更改父视图的左边距将同时影响所有子视图。
回答by KPM
In iOS 8 you now have the option to define your constrains relative to a predefined margin to the superview's bounds, instead of the superview's bounds themselves. Yes, it is totally related to the layout margins you pointed to in the docs. One advantage is that you may redefine your margins dynamically, or differently for each kind of device, and the layout will be updated correspondingly without modifying the constraints.
在 iOS 8 中,您现在可以选择定义相对于超视图边界的预定义边距的约束,而不是超视图边界本身。是的,它与您在文档中指出的布局边距完全相关。一个优点是您可以动态重新定义边距,或者针对每种设备进行不同的重新定义,并且布局将相应地更新而无需修改约束。
When to use it: when you want to take advantage of this new flexibility.
何时使用:何时您想利用这种新的灵活性。
When to NOT use it: for any app targeted to run on iOS 7 or below.
何时不使用它:适用于任何要在 iOS 7 或更低版本上运行的应用程序。
回答by Kyle Robson
The property on UIView is: layoutMargins
. See the Apple Docs. Basically if the layout margins are 8,8,8,8 (the default), a constraint with 0 leading space to container margin will have an x position of 8. Note that this is only available on iOS8 or later.
UIView 上的属性是:layoutMargins
。请参阅Apple 文档。基本上,如果布局边距为 8,8,8,8(默认值),则容器边距前导空间为 0 的约束的 x 位置将为 8。请注意,这仅适用于 iOS8 或更高版本。
For everyone who doesn't want their constraints to go to the container margin:
对于不希望他们的约束进入容器边缘的每个人:
CTRL+click+drag to show the constraint creation popup.
CTRL+单击+拖动以显示约束创建弹出窗口。
If the menu shows to create the constraint to the margin by default, hold down option/alt to allow the constraint to be made to the container and not the container margin.
如果菜单显示默认创建对边距的约束,请按住 option/alt 以允许对容器而不是容器边距进行约束。
Now it will show the option to create the constraint NOT to the margin. This is WAYfaster in my usage.
现在它将显示创建约束而不是边距的选项。这是WAY我的使用速度更快。