Search code examples
iosxcodeautolayout

autolayout constraints not placing container view properly


The first image shows the constraints on the bottom container view. "Bottom space to superview" is set to 0. I want the container to be anchored to the bottom of the screen no matter what the screen height.

I do not want a constraint on the distance from the bottom container view to the top of the screen, but Xcode is creating one automatically (top space to superview = 508) and won't let me delete it. Why is it doing that? My container view's position is fully defined by the other 3 constraints and its height (60 px).

visual

More importantly, when I run the application, the container view does not fit on screen. It is partly cut off. The bottom of the container view is cut off even though "bottom space to superview" is constrained to be 0. What is going on?

visual


Solution

  • Two things are happening. First, your container view's height is set, but NOT PINNED, so autolayout is forcing the topspace to superview to set. You need to pin its height, and then you can delete the topspace, and that problem should disappear.

    Second, the status bar at the top is forcing your view down 20 pixels, which is pushing its bottom off the screen. There are various fixes for this, but I'm sure now that you know the problem, you'll come up with one.