1

I have the following, simple layout: just a scroll view, with some views in it (please excuse the preview screenshot, since I'm using XIBs, there is no way to zoom out):

layout

Here is my view hierarchy + constraints (the vertical constraints are all set up, throughout the view):

hier_1

My problem with this case is that I get the following error in Interface Builder:

ib_error

I've read up on the issue, I've found that I would need to add my scroll view as a subview to a simple view to silence this warning, but couldn't make it work (the view didn't scroll for some reason).

I think I understand the error here: the scroll view doesn't know, how far it can scroll horizontally, but I've found no way to specify this in IB (instead of setting it to a concrete value - which I don't want).

The problem is even more obvious, when I try to add another view to my hiearachy, and constrain its leading, trailing, and bottom edges to the scroll view:

scrollgif

The view just can't figure out its width, thus the messed up scrolling.

My question is the following: is there a good way to get rid of this ambiguity? In Storyboards, you can just add equal height/width constraints to the root view, but this is not an option with XIBs.

4
  • check out natashatherobot.com/ios-autolayout-scrollview
    – Bharat
    Commented Sep 8, 2015 at 8:56
  • @Bharat, thanks for the comment, but if you've read the question, this link is already included. :) Commented Sep 8, 2015 at 8:56
  • i don't see this link in your question. anyway have you tried the steps described inn the post?
    – Bharat
    Commented Sep 8, 2015 at 9:00
  • @Bharat yes, In the comments, someone suggested adding a view as superview: this way, the warning goes away, but scrolling is not working. Commented Sep 8, 2015 at 9:02

1 Answer 1

1

Okay, so I've managed to solve the issue with the following steps:

  1. Use the following hierarchy: View of the xib ("main view") -> Additional view ("root view") -> Scroll view -> Additional view ("content view")

hierarchy

  1. Pin the root view's edges to the main view so the screen is filled. Do the same for the scroll view (pin its edges to the root view's).
  2. Define an "Equal Widths" constraint between the root view, and the content view. It's important to only constrain the width. My mistake was to also constraint the height, which caused the scroll view not to scroll.
  3. Add any child views to the content view. Important note: you have to define the proper vertical constraints between the children, otherwise, the content view's height will be zero.

vertical

And that should be it! No more errors, and definitely no awkward scrolling glitches.

final

Not the answer you're looking for? Browse other questions tagged or ask your own question.