Xcode: Trick shows how to create constraint to Top of UIViewController's view instead of Top To Layout Guide

This is a pretty useful trick that my colleague Piotr Tobolski shown me recently when I was struggling with Auto Layout (mainly cleaning it up).

When you press and hold Control and try to attach top of a subview to UIViewController’s view you can see you’re able to create few types of them and there is a “Vertical Spacing to Top Layout Guide” for example, but there is no a “Vertical Spacing to Top” which you probably want to use when you need to place a map on the entire view or you want to place some view filled with shadow (gradient from black to transparent) from the top of the screen under status bar.


The same issue is when you try to create constraint to bottom, You can create only “Vertical Space to Bottom Layout Guide” with such a simple drag - can’t see “Vertical Spacing to Bottom”.


But hey, this is possible to create such a constraint. What you need to do is to create e.g. “Center Vertically in Container” constraint, select it, and change its items to “Superview.Top” which is UIViewController’s view and another item to “YourViewOnStoryboard.Top”.


“Constraint to Bottom” can be created respectively by changing “Top” to “Bottom”.

Thanks Piotr!