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.

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.

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.

How can I manually do this in storyboard or do I have to add it programatically?

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


This is outdated in XCode 7, see @PaulGurov's answer instead.


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

在这里输入图像描述

To do that you just Control + Drag from 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.

在这里输入图像描述

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

在这里输入图像描述

链接地址: http://www.djcxy.com/p/28290.html

上一篇: 垂直对齐UILabel中的文本

下一篇: Autolayout:将约束添加到超级视图而不是顶部布局指南?