什么是"约束保证金"在X code 6故事板 [英] What is "Constrain to margin" in Storyboard in Xcode 6

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

问题描述

我与自动布局和制约的工作,发现有一个约束边距在X code 6选项,是不是X中$ C present $ C 5,默认情况下选中。

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

X code 6
你可以在这里看到,即使tableview中具有相同的帧视角X code暗示加-16为约束,而X code 5会建议增加间距0。

现在,当您取消约束保证金选项,它的表现一样X code 5,并会建议加0为约束

此外,我发现,一旦我与约束添加约束保证金检查,我不再能够在X code 5,打开故事板文件,以便其definatly英寸x code一些新的6

我希望我能好好解释一下我的问题。我想了解什么是约束保证金实际上做,当我应该和不应该使用它。我道歉,如果这件事情很浅显。

修改

我发现了一些关于布局利润率<一个href=\"https://developer.apple.com/LIBRARY/$p$pRELEASE/IOS/documentation/UIKit/Reference/UIView_Class/index.html#//apple_ref/occ/instp/UIView/layoutMargins\">discussion这里,我不知道它是否与此有关。


解决方案

我不为什么人们都在抱怨说,利润率将造成什么彻底崩溃之前到iOS 8理解的。


  

设置您的约束相对利润率在厦门国际银行文件或故事板的就iOS7您的应用程序崩溃,并且它的让你的iOS7设备上的用户界面差异两者都不是,只要你不碰 UIView.layoutMargins 的UIView。preservesSuperviewLayoutMargins 属性您code。


什么是iOS8上边距

布局利润率重新围绕在内部 present填充的的UIView 的布局子视图时,布局系统可以使用 - 保证一个缺口视图和子视图的边缘之间留下。在这方面,它很像在CSS中的块相关联的填充属性

在这里输入的形象描述

默认情况下,的UIView 有每侧8点布局的利润,而这不能在界面生成器来改变。然而,通过设置code,这是只有在iOS8上可用的 UIView.layoutMargins 属性,你可以调整这些值。


  

您可以IB与编辑>画布>显示布局矩形显示边距:
  


页边距可以用来帮助您的布局视图和子视图。每个的UIView 皆预设利润,但是当你设置一个关系到一个保证金约束他们只影响视图的位置。

如何使用边距

在Interface Builder中使用的利润的唯一方法是在配置您的约束检查相对于保证金选项。这是你如何引导你的约束的使用的利润率,而不是边缘铺设了我的看法时。

在这里输入的形象描述

让我们来看看在视图及其子视图之间建立一个领先的约束四种不同的方式。对于每一个约束,我们回顾说明将子视图领先在第一个协会,并在第二个将是上海华领先。要抓紧什么检查,并在相对于保证金每个约束结束的选项取消选中状态,因为定义约束是否是联系在一起的保证金或视图的边缘。


  1. 第一项(选中),第二项(检查):在这种情况下,我们宣布子视图的左边缘应对准上海华的左边缘(如在此图像中)

在这里输入的形象描述

<醇开始=2>
  • 第一项(选中),第二项(选中):使用两个边的不保证金。在这种情况下,我们宣布子视图的左边缘应该对准上海华的左边缘。

  • 在这里输入的形象描述

    <醇开始=3>
  • 第一项(检查),第二项(选中):在这种情况下,我们宣布子视图的左边缘应该对准上海华的左边缘。这种布局实际上使子视图重叠的SuperView。

  • 在这里输入的形象描述

    <醇开始=4>
  • 第一项(检查),第二项(检查)。这实际上已经为2的情况下同样的效果,因为这两个子视图和上海华有一个相同的默认保证金。我们声明子视图的左边缘应该对准上海华的左边缘。

  • 在这里输入的形象描述

    什么是良好边距

    此新功能(iOS8上),如果你决定使用利润只会影响UI开发。

    通过使用利润率可以调整,通过改变单一属性的值都有一个共同的关系到一个共享的SuperView多子视图的位置。这是在设置所有相关的约束带固定值,因为如果你需要更新所有的间距,而不是由一个来改变每个值一个明确的胜利,你可以同时通过单行更新的SuperView的保证金修改所有相关位置code像这样的:

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

    为了说明这一优势,在以下情况下,所有子视图左边缘对齐其上海华的左边缘。因此,改变上海华的左缘将影响所有子视图在同一时间。

    在这里输入的形象描述

    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.

    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 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 to add spacing 0.

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

    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 its definatly 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.

    EDIT

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

    解决方案

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

    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.

    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.

    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.

    You can get IB to display the margins with 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.

    How to use Margins

    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.

    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. 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).

    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. 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.

    1. 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.

    What is good about Margins

    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);
    

    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.

    这篇关于什么是&QUOT;约束保证金&QUOT;在X code 6故事板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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