Autolayout: Add constraint to superview and not Top Layout Guide? Autolayout: Add constraint to superview and not Top Layout Guide? ios ios

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


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.

enter image description here


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.


TL;DR: You can constrain to the margin by pressing the Alt key on the keyboard.


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

enter image description here

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.

enter image description here

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

enter image description here