0

I'm having trouble getting a UIScrollView to respect the constraints I put in interface builder.

All I need to be able to do is set the content size of the scroll view from within IB.

The UIScrollView contains a single UIView.

Constraints on the UIScrollView:

Scroll View Constraints

Constraints on the UIView:

enter image description here

I've read through the documentation, and so have set things up as follows:

  • the UIScrollView has constraints pinning it to its superview, thus defining its size from outside
  • the UIView (content) has a fixed size (through width and height constraints)
  • the UIView is pinned to the UIScrollView, thus defining the content size

However, IB won't let me enter these constraints. If I change the 'Bottom Space' constraint between the view and the scroll view, shown in the image as -2196, to 0 (thus pinning the lower edge of the scroll view), then the 'Top Space' constraint resets to a non-zero value. The same happens in reverse. (I haven't yet tried in Xcode 5, which has a far saner approach to invalid constraints in that it doesn't just throw yours away when it feels like it.)

What am I missing?

sapi
  • 9,944
  • 8
  • 41
  • 71

1 Answers1

0

Every time I've tried to do something even mildly sophisticated with constraints in Xcode 4's Interface Builder, I've eventually given up and either written the constraints in code or switched back to springs'n'struts and layoutSubviews (usually after crashing Xcode a few times).

That said, there is another approach to laying out a scroll view with content in IB. Just make the scroll view as big as its content size, and rely on the view controller (or some containing view controller) to resize the scroll view (or its superview) and let the constraints shrink down the scroll view's frame at runtime. The window's root view controller will always set its view's frame to the screen size, regardless of its size in the nib or storyboard, and that resizing flows down the view hierarchy.

I described this approach in more detail in this answer.

If your scroll view's content size is really supposed to be 2196 points tall, this probably won't work so well. I don't have anything better to suggest in that case.

Community
  • 1
  • 1
rob mayoff
  • 375,296
  • 67
  • 796
  • 848
  • I completely agree with you. The way Xcode 4 attempts to enforce constraints 100% of the time (rather than allowing breaking changes that you fix before compilation) is the real problem. As an example, I just tried adding size constraints to the `UIScrollView`, and then removing it...and that configuration (identical to the one that I was trying originally) was permitted > – sapi Jun 30 '13 at 04:33
  • 1
    Yes that is what you have to do in Xcode 4, over constrain then delete else Xcode will fight you by adding random constrains because it will lose it's mine if some thing is under constrained. – GayleDDS Jul 01 '13 at 01:02