Search code examples
iosiphoneswiftautolayoutnslayoutconstraint

How can I add a contraint to the root view instead of TopLayoutGuide?


My ViewController is embedded in a UINavigationController, and it has a view that should be aligned with the top of root view. The root view has dark gray background, and occupies full screen. But this is what it looks like now:

enter image description here

As you can see, the root view is displayed below the status bar, which is expected. But the subview (black) is displayed below a white strip. I guess this is the top layout guide, and my subview has a constraint with its top equal to the bottom of the top layout guide. After I remove this constraint, I cannot add a constraint from my subview to the root view by Ctrl-dragging, Xcode always set up the constraint with top layout guide.

My question is:

  1. How can I add a constraint top of subview == top of root view?
  2. Why does the top layout guide occupy the white region?

Solution

  • self.automaticallyAdjustsScrollViewInsets = true
    

    The default value of this property is true, which lets container view controllers know that they should adjust the scroll view insets of this view controller’s view to account for screen areas consumed by a status bar, search bar, navigation bar, toolbar, or tab bar. Set this property to false if your view controller implementation manages its own scroll view inset adjustments.

    You can also set this property from Interface Builder