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

47,131

Solution 1

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

Solution 2

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.

Solution 3

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

Solution 4

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.

Select the relative to margin

Solution 5

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:

enter image description here

enter image description here

Share:
47,131

Related videos on Youtube

Arbitur
Author by

Arbitur

Been working as a Mobile App Developer for iOS and Android since 2014. Loves problem solving and learning new things. 100% self taught so I really like what I do, its my job and part of my hobbies. I recently (as of 2018) started with 3DPrinting and also have years of CAD experience.

Updated on February 13, 2020

Comments

  • Arbitur
    Arbitur over 4 years

    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.

  • NKorotkov
    NKorotkov almost 9 years
    Works like a charm - XCode 6.3
  • Kyle G
    Kyle G almost 9 years
    They've removed 'pin' from the editor menu in Xcode 7. Any new solutions?
  • Jeremy Hicks
    Jeremy Hicks over 8 years
    Hallelujah! I didn't know what I was going to do when they removed the Editor-->Pin menu.
  • zumzum
    zumzum over 8 years
    can't believe I didn't see that before. Awesome.
  • Ryan Pfister
    Ryan Pfister over 8 years
    Just a note for people who are as clueless as me --> you may not be able to make this change unless the view you are adding the constraint to is below the top layout guide. I had a view above the top layout guide and "Top Layout Guide" wasn't an option in the drop down menu. Which is probably a "duh" statement for non-newbs like me :)
  • Marchy
    Marchy over 8 years
    It's about those small Xcode subtleties. That do SO much. :)
  • micnguyen
    micnguyen over 8 years
    Just a quick clarification that confused me: despite the image having Top Layout Guide ticked, if you want to pin the view to the TOP of the view like the very top of the device, then select "View".
  • netdigger
    netdigger about 8 years
    This was what I was looking for when I came here! Thanks
  • Daniel Wood
    Daniel Wood about 8 years
    Is this only available in storyboards and not plain nibs? I cannot get this option to appear for love nor money.
  • Pavel Gurov
    Pavel Gurov about 8 years
    @DanielWood Well, the 'topLayoutGuide' and 'bottomLayoutGuide' are available anywhere in the IB (storyboard or a xib) as long as what you are editing is a viewController's view, not a regular UIView. If it is a regular UIView you still get the arrow, only the options will be limited to superview and neighbouring views, because plain UIViews don't have layout guides. btw, thanks a lot for Seconds app, its simply the best, I use it almost every day for my boxing workouts!
  • Daniel Wood
    Daniel Wood about 8 years
    @PaulGurov Ha, thanks. I'm glad you like it. Regarding the nibs. I realised I needed to add a view controller in the end. But I couldn't get it to work with UIViewController initWithNib... Are the two not compatible? Look at me, has successful app, still can't code ;-)
  • Daniel Wood
    Daniel Wood about 8 years
    @PaulGurov Thanks for the offer. I'm just prototyping right now so I'll just use Storyboards instead. Or just do it in code like I always have.
  • Ryan Chou
    Ryan Chou almost 8 years
    can we see it default to the superview?
  • Pavel Gurov
    Pavel Gurov almost 8 years
    Even if there is a way to do that, I am not aware of it (
  • AnBisw
    AnBisw almost 7 years
    Awesome, but I wonder why it sometimes aligns top (and bottom) automatically to Superview and some time it does it to the Top Layout guide. 🤔
  • Machado
    Machado over 4 years
    Very useful tip. This has actually solved my problem.
  • Duc Nguyen
    Duc Nguyen about 4 years
    It works for me, but I don't know how to add multiple constraints at the same time. Now, I have to add one by one, to take time.