Search code examples
iosautolayoutxcode7

How to pin to TopLayoutGuide.Top xcode 7


This used to work in xcode 6, and is now grayed out in Xcode 7. Can someone explain why .Bottom is my only option now?

enter image description here


Solution

  • I often want this for showing scrollviews below the navigation bar. Apple probably has a good reason to remove it and iOS 11 is getting rid of layout guides anyway, but for academic purposes you can still accomplish this.

    First, start with a constraint between your view's top and the bottom of the top layout guide.

    Second, get the object Id of your view from the Document section of the Identity Inspector (select your view in Interface Builder and type Alt-Cmd-3). In my case c9Q-8h-rHu. enter image description here

    Third, open your storyboard as source code by Ctrl-clicking the file and choosing "Open as > Source Code" enter image description here

    Now search for your object Id. In the results of that search look for a constraint where your view's top is constrained to some other object's bottom.

    <constraint firstItem="c9Q-8h-rHu" firstAttribute="top" secondItem="pgx-wv-BwY" secondAttribute="top" id="OSD-K0-laA"/>
    

    Change the second attribute to top and save. You can then Ctrl-click again on your file and choose to open as Interface Builder Storyboard.

    Note that the constraint is changed to a "Align Top", but it's functionally equivalent.