Note that there are some explanatory texts on larger screens.

plurals
  1. POScrolling, zooming UIScrollView and interface orientation rotation. How to use autoresize and more
    primarykey
    data
    text
    <p>This should be a pretty common thing to do, but I haven't been able to get it to work exactly right.</p> <p>I have rectangular content. It normally fits in 320x361: portrait mode minus status bar minus ad minus tab bar.</p> <p>I have put that content in a UIScrollView and enabled zooming. I also want interface rotation to work. The content will always be a tall rectangle, but when zoomed users might want to see more width at a time and less height.</p> <p>What do I need to do in Interface Builder and code to get this done? How should I set my autoresizing on the different views? How do I set my contentSize and contentInsets?</p> <p>I have tried a ton of different ways and nothing works exactly right. In various of my solutions, I've had problems with after some combination of zooming, interface rotation, and maybe scrolling, it's no longer possible to scroll to the entire content on the screen. Before you can see the edge of the content, the scroll view springs you back.</p> <p>The way I'm doing it now is about 80% right. That is, out of 10 things it should do, it does 8 of them. The two things it does wrong are:</p> <ol> <li><p>When zoomed in portrait mode, you can scroll past the edge of the content, and see a black background. That's not too much to complain about. At least you can see all the content. In landscape mode, zoomed or not, seeing the black background past the edge is normal, since the content doesn't have enough width to fill the screen at 1:1 zoom level (the minimum).</p></li> <li><p>I am still getting content stuck off the edge when it runs on a test device running iOS 3.0, but it works on mine running 4.x. -- Actually that was with the previous solution. My tester hasn't tried the latest solution.</p></li> </ol> <p>Here is the solution I'm currently using. To summarize, I have made the scroll view as wide and tall as it needs to be for either orientation, since I've found resizing it either manually or automatically adds complexity and is fragile.</p> <p>View hierarchy:</p> <ul> <li>view <ul> <li>scrollView <ul> <li>scrollableArea <ul> <li>content</li> </ul></li> </ul></li> <li>ad</li> </ul></li> </ul> <p>view is 320x411 and has all the autoresizing options on, so conforms to screen shape</p> <p>scrollView is <strong>480</strong> x 361, starts at origin -80,0, and locks to top only and disables stretching</p> <p>scrollableArea is <strong>480</strong> x 361 and locks to left and top. Since scrollView disables stretching, the autoresizing masks for its subviews don't matter, but I tell you anyway.</p> <p>content is 320x361, starts at origin 80,0, and locks to top</p> <p>I am setting <code>scrollView.contentSize</code> to 480x361.</p> <p><code>shouldAutorotateToInterfaceOrientation</code> supports all orientations except portrait upside down.</p> <p>In didRotateFromInterfaceOrientation, I am setting a bottom content inset of 160 if the orientation is landscape, resetting to 0 if not. I am setting left and right <em>indicator</em> insets of 80 each if the orientation is portrait, resetting if not.</p> <pre><code>scrollView.minimumZoomScale = 1.0 scrollView.maximumZoomScale = 2.0 </code></pre> <p>viewForZoomingInScrollView returns scrollableArea</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload