Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 930
Arbitur Avatar asked Feb 27 '15 13:02

Arbitur


People also ask

How do you add constraints to a storyboard?

Select the view you would like to constrain. Then tap the button to the right of the one you have selected and use that menu to define your autolayout constraints. If you want it to work for all devices make sure your storyboard is on the wAny hAny size class setting.

How do I create a constraint in Autolayout?

There are three main options for setting up Auto Layout constraints in Interface Builder: You can control-drag between views, you can use the Pin and Align tools, and you can let Interface Builder set up the constraints for you and then edit or modify the results.

How do I give a constraint to button programmatically in Swift?

Add the button in the view, give it constraints and as you are using constraints, you can skip the button. frame and add widthAnchor and heightAnchor . At last activate them and keep translatesAutoresizingMaskIntoConstraints as false . Also, it will be better if you can add proper names.


3 Answers

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

like image 181
Pavel Gurov Avatar answered Oct 12 '22 02:10

Pavel Gurov


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.

like image 58
dan Avatar answered Oct 12 '22 02:10

dan


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

like image 15
Zhivko Bogdanov Avatar answered Oct 12 '22 04:10

Zhivko Bogdanov