Note that there are some explanatory texts on larger screens.

plurals
  1. POUIScrollView + change orientation = messed up subviews (iPad)
    primarykey
    data
    text
    <p>I want to use a <code>UIScrollView</code> as my main container in the app, enabling me to swipe back and forth between subviews. To achieve this, I created a <code>UIViewController</code> subclass with a <code>UIScrollView</code> <strong>IBOutlet</strong>:</p> <p>In the <code>viewDidLoad</code> method I construct the sub-pages:</p> <pre><code>for (int i= 0; i&lt; pageCount; i++) { CGRect frame = self.scrollView.frame; frame.origin.x = frame.size.width * i; frame.origin.y = 0; UIWebView* aWebView= [[UIWebView alloc] initWithFrame:frame]; [self.scrollView addSubview:aWebView]; } </code></pre> <p>When launching the app (portrait mode), everything works. That is, the <code>UIWebViews</code> are layed out side by side with the correct dimensions, and I can swipe back and forth between them.</p> <p>When I rotate to landscape, it seems that neither the scrollview size nor the subviews are resized.</p> <p>I don't know what I should do in order to resize the subviews and the scrollview itself, or at what point in code I should do anything, and I cant seem to find any examples for this.</p> <p>Anyone know what to do?</p> <p>[edit] Attempt to adjust sizes as suggested by mahboudz:</p> <pre><code>- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation { self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * pageCount, self.scrollView.frame.size.height); for (int i= 0; i&lt; pageCount; i++) { CGRect frame = self.scrollView.frame; frame.origin.x = frame.size.width * i; frame.origin.y = 0; UIWebView* view= [[self.scrollView subviews] objectAtIndex:i]; view.frame= frame; } } </code></pre> <p>This kind of does what I want, but has the following issues:</p> <p>1) one can see the subviews grow to correct screen size upon changing orientation</p> <p>2) when the current page is, for example, page 2 of 5 pages, the page is not fully visible after orientation was changed, but is off-screen by like 40 pixels</p> <p>3) I get strange effects depending on whether the app is launched in portrait or landscape mode (simulator), ill try to explain:</p> <p>When the app is launched in portrait mode:</p> <ul> <li>The shape/border of the subviews looks messed up/offscreen, see screenshots:</li> </ul> <p><a href="http://i53.tinypic.com/21jr76x.png" rel="nofollow">http://i53.tinypic.com/21jr76x.png</a></p> <ul> <li>when I rotate to landscape, everything looks okay, scrolling works superb. even when I rotate back to portrait, everything is great now:</li> </ul> <p><a href="http://i55.tinypic.com/if3iiw.png" rel="nofollow">http://i55.tinypic.com/if3iiw.png</a></p> <p>When the app is launchend in landscape mode:</p> <ul> <li><p>I get the same messed up/offscreen glitches as in portrait mode</p></li> <li><p>Switching back and forth between portrait and landscape fixes this for landscape mode</p></li> <li><p>BUT: Portrait mode will have the subviews with the width of the landscape mode, thus subviews are too wide</p></li> </ul> <p>I tried to fix 1) doing the code above in <code>willRotateToInterfaceOrientation</code> however it completely messed up the layout.</p> <p>I fixed 2) by adding the following code to <code>didRotateFromInterfaceOrientation</code>:</p> <pre><code>// update the scroll view to the appropriate page CGRect frame = self.scrollView.frame; frame.origin.x = frame.size.width * self.currentPage; frame.origin.y = 0; [self.scrollView scrollRectToVisible:frame animated:NO]; </code></pre> <p>Note: current page is determined in <code>scrollViewDidScroll</code></p> <p>I dont have any idea how to fix 3)</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.
    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