简体   繁体   English

Autolayout:为superview添加约束而不是Top Layout Guide?

[英]Autolayout: Add constraint to superview and not Top Layout Guide?

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-50px然后发生的是我不会移动的视图...因为它不与superview对齐。

How can I manually do this in storyboard or do I have to add it programatically? 如何在故事板中手动执行此操作或者是否必须以编程方式添加它?

Im using xcode 6. 我正在使用xcode 6。

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. 如果您选择“查看”,则会将其固定到视图的顶部。

在此输入图像描述

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的答案。

TL;DR: You can constrain to the margin by pressing the Alt key 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 + Drag from the Parallax Image View to the View . 要做到这一点,你只需从Parallax Image View 控制 + 拖动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. 这将显示默认选项,您可以在其中查看“ 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键切换到另一组约束

在此输入图像描述

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”的项目,则可以通过选择该项目来关闭“相对于保证金”。

选择相对于保证金

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的视图,并通过引脚面板配置约束,它将设置为superview:

在此输入图像描述

在此输入图像描述

If the height of the view is set to 0 by accident, Vertical Space to Top Layout Guide will NOT appear. 如果视图的高度意外设置为0,则不会显示“垂直空间到顶部布局指南”。 Vertical Space to Bottom Layout Guide will appear, however, which must have made sense to someone inside Apple circa 2004. 然而,将出现垂直空间到底部布局指南,这对于2004年左右的Apple内部人员来说必定是有意义的。

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. 上述答案都不适合我,相反,我必须做一个黑客来设置我的子视图的顶部与superview的顶部而不是顶部布局指南。

  • First, set the subview center vertically with superview. 首先,使用superview垂直设置子视图中心。
  • Then, change the Center Y constraints for subview as well as superview to Top constraint. 然后,将子视图的中心Y约束以及超视图更改为顶部约束。

You are good to go now. 你现在很高兴。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 UINavigationController和自动布局顶部布局指南的异常行为 - strange behaviour with UINavigationController and autolayout top layout guide 顶部和底部布局指南的自动布局问题 - Autolayout issue with top & bottom layout guide 有没有办法在xib文件中的视图和顶部布局指南之间添加约束? - Is there any way to add constraint between a view and the top layout guide in a xib file? 从nib尊重superview的顶部布局指南(导航栏底部)加载视图 - Make view loaded from nib respect superview's top layout guide (Navigation Bar Bottom) 如何以编程方式添加自动布局约束以使按钮在其视图底部上方50点? - How can I programmatically add an autolayout constraint to make a button 50 points above bottom of its superview? iOS 8自动布局-视觉格式语言如何向超级视图的边缘添加约束 - iOS 8 Autolayout - Visual Format Language how to add constraint to superview's edge 如何使用具有垂直间距的自动布局到subview.top而不是Top布局指南 - How can I use autolayout with vertical spacing to subview.top but not Top layout Guide iOS 7 AutoLayout“顶部到顶部布局指南”是否在Interface Builder中没有控制拖动? - iOS 7 AutoLayout “Top Space to Top Layout Guide” Without Control-Drag in Interface Builder? 顶部布局指南不匹配 - Top layout guide mismatch 在Xcode 5中为superview添加间距约束 - Add spacing constraint to superview in Xcode 5
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM