ios 自动布局:向超级视图添加约束而不是顶部布局指南?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28766210/
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
Autolayout: Add constraint to superview and not Top Layout Guide?
提问by Arbitur
I have a UIView in my UIViewController in storyboard which I want to add a constraint on to space that view a distance from the top edge.
我在故事板的 UIViewController 中有一个 UIView,我想在空间上添加一个约束,从顶部边缘查看距离。
Now, when I do the usual by ctrl + drag to the ViewController's main view I only get option to set it to top layout guide.
现在,当我通过 ctrl + 拖动到 ViewController 的主视图执行通常的操作时,我只能选择将其设置为顶部布局指南。
This is a problem for me because at one point in the app Im going to move the main view up around 20-50px and what happens then is that view I have will not move... because its not aligned to superview.
这对我来说是一个问题,因为在应用程序的某个时刻,我将主视图向上移动 20-50 像素,然后发生的情况是我拥有的视图不会移动......因为它没有与超级视图对齐。
How can I manually do this in storyboard or do I have to add it programatically?
如何在故事板中手动执行此操作,还是必须以编程方式添加?
Im using xcode 6.
我使用 xcode 6。
回答by Pavel Gurov
There is an arrow to the right of the constraint constant value. If you click on it, a menu pops out where you can choose what is it that you want to make your constraint relative to. If you choose 'View', than it will pin it to the top of the view.
约束常量值的右侧有一个箭头。如果您单击它,则会弹出一个菜单,您可以在其中选择要相对于哪些内容进行约束。如果您选择“查看”,它会将其固定在视图的顶部。
回答by dan
You should be able to do it by highlighting the view you want in the storyboard and then selecting Editor > Pin > Top Space to Superview
in the top menu.
您应该能够通过在故事板中突出显示您想要的视图,然后Editor > Pin > Top Space to Superview
在顶部菜单中进行选择来实现。
This is outdated in XCode 7, see @PaulGurov's answer instead.
这在 XCode 7 中已过时,请参阅 @PaulGurov 的回答。
回答by Zhivko Bogdanov
TL;DR: You can constrain to the margin by pressing the Altkey on the keyboard.
TL;DR:您可以通过按Alt键盘上的键来限制边距。
One can also add that if the view you are trying to pin is not a direct subview of the view that you want to pin it to, the suggested approach is not really valid. Let's say the view hierarchy looks like that and you want to pin the image view (Parallax Image View
in this case) to the view controller's view (View
in this case).
还可以补充一点,如果您尝试固定的视图不是要固定到的视图的直接子视图,则建议的方法实际上无效。假设视图层次结构看起来像这样,并且您想将图像视图(Parallax Image View
在本例中)固定到视图控制器的视图(View
在本例中)。
To do that you just Control+ Dragfrom the Parallax Image View
to the View
. That presents the default choices where you can see Vertical Spacing to Top Layout Guide
to pin the Parallax Image View
to the top layout guide.
要做到这一点,您只需Control+Drag从Parallax Image View
到View
. 这提供了默认选项,您可以在其中看到Vertical Spacing to Top Layout Guide
将其固定Parallax Image View
到顶部布局指南。
What you want is to pin it to the top (margin) of the view. To do that you have to switch to the other set of constraints by pressing Alt
您想要的是将其固定到视图的顶部(边距)。为此,您必须通过按切换到另一组约束Alt
回答by christophercotton
I had a whole bunch of constraints already there and I just wanted to change them rather than break them. Turns out if you double click on the constraint to edit, then click on the item that has the ".Margin", you can just turn off the "Relative to Margin" by selecting that item.
我已经有了一大堆约束,我只是想改变它们而不是打破它们。事实证明,如果您双击约束进行编辑,然后单击具有“.Margin”的项目,您可以通过选择该项目来关闭“相对于保证金”。
回答by oren
Dan's answer works. I just wanted to state that if you set your view in storyBoard at y < 20, and configure constraints through the pin panel, it will set top to superview:
丹的回答有效。我只是想说明,如果您将 storyBoard 中的视图设置为 y < 20,并通过 pin 面板配置约束,它会将顶部设置为超级视图:
回答by Gabriel Jensen
If the height of the view is set to 0 by accident, Vertical Space to Top Layout Guide will NOT appear. Vertical Space to Bottom Layout Guide will appear, however, which must have made sense to someone inside Apple circa 2004.
如果不小心将视图的高度设置为 0,则不会出现垂直空间到顶部布局指南。然而,将出现垂直空间到底部布局指南,这对于大约 2004 年左右的 Apple 内部人员来说一定是有意义的。
回答by Jas_meet
None of the above answers worked for me, instead I have to do a hack to set my subview's top with superview's top instead with Top layout guide.
以上答案都不适合我,相反,我必须做一个 hack 来设置我的子视图的顶部与超级视图的顶部,而不是顶部布局指南。
- First, set the subview center vertically with superview.
- Then, change the Center Yconstraints for subview as well as superview to Topconstraint.
- 首先,用父视图垂直设置子视图中心。
- 然后,将子视图和超视图的Center Y约束更改为Top约束。
You are good to go now.
你现在可以走了。