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.
- Best pratice between storyboard xib/nib pure programming
- Xcode 8 UIButtons with constraints not showing up
- Popover size with UINavigationController in Storyboard
- Storyboard - Unsupported configuration 8 conflicting constrains
- ios Storyboarding - Dismissing a modal?
- “Unbalanced calls to begin/end appearance transitions” warning when push a view in a modal way in XCode 4 with Storyboard
7 Solutions Collect From Internet About “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.
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
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.
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:
If the height of the view is set to 0 by accident, Vertical Space to Top Layout Guide will NOT appear. Vertical Space to Bottom Layout Guide will appear, however, which must have made sense to someone inside Apple circa 2004.
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.
- First, set the subview center vertically with superview.
- Then, change the Center Y constraints for subview as well as superview to Top constraint.
You are good to go now.
- iOS Stream Audio from one iOS Device to Another
- Changing Navigation Controller colour swift
- Highlight active link in SWRevealViewController
- Incorrect UIView bounds/drawing in the Simulator
- How to handle more than one location on the same address and cluster them?
- Valid use of accessors in init and dealloc methods?
- How can I load a default language plist if the localized version does not exist for the current language?
- Autosynthesized property 'delegate' will use synthesized instance variable '_delegate', not existing instance variable 'delegate'
- Using Simple Ping in swift (iOS)
- UIButton w/ gradient, rounded corners, border, and drop shadow
- App Rejected on 17.2 clause. Asking for email ID
- Will always send completion block?
- SpriteKit, Swift 2.0 – ScrollView in reverse
- UIScrollView pauses NSTimer while scrolling
- How Can I make & send array of dictionary (JSON Format ) send to server using Alamofire in swift 3