Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I know that a scroll view can determine it's contentSize based on the way its subviews are constrained. I have a slightly more tricky situation that I would like to resolve using auto layout.

I am using a paging scroll view, similar to that in the weather app. But I am lazily loading each page of content when it is required. Previously I simply set the contentSize width of the scrollview to x * the scrollview's width, where x is the number of pages of content. Then the origin of each page is offset depending on the page number.

With autolayout I need to add a page of content and pin it to an edge of the scrollview, so that it can determine the correct content size. If I am loading page 3 of 6, the trailing space to the edge of the scrollview would be 3 X the scrollview width. A similar leading constraint can be determined.

Visually, it is similar to the following (pseudocode):

|-(scrollView.width*pageNumber)-[page]-(scrollview.width * (pages.count - pageNumber))-|

Thus placing the page in correct location, with sufficient left and right margins so that the scrollview knows its content size.

What is the correct way to add such a constraint, such that:

  • no magic numbers are used
  • the layout will work on rotation

I have tried multiple approached but can't quite get it.

share|improve this question
    
I hesitate to add an answer since I don't have anything definitive, but it feels like the number you're looking for should be available since something is on the screen. Maybe the scrollview's frame size? Or the root view controller's size? What approaches have you tried that do not work? Sharing those may help jostle an idea loose and avoid covering the same ground. –  James Aug 20 '13 at 15:06
add comment

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.