Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h2>Please note: the bug this question and answer is about appears to be fixed in iOS 7. The rest of this answer is only relevant to iOS 6 (and probably earlier).</h2> <p>The behaviour being exhibited here is a bug in the <code>UIScrollView</code> class. As noted by the OP, after returning from a modally presented <code>UIViewController</code> to a scene containing a <code>UIScrollView</code>, the <code>UIScrollView</code> takes whatever point it's currently scrolled to and starts behaving as though that is its origin. That means that if you'd scrolled down your scroll view before modally presenting another View Controller, you can't scroll back up upon returning to the scene with the scroll view.</p> <p>The same thing happens when you remove the Scroll View from the view hierarchy and re-add it, even without changing its window.</p> <p>You can work around this by setting the <code>contentOffset</code> of the scroll view back to <code>{0,0}</code> before it gets displayed again after dismissing the modal View Controller. If you actually want to preserve the point the user had scrolled to before they triggered the modal, then <em>after</em> the <code>UIScrollView</code> is redisplayed you can set the <code>contentOffset</code> back to whatever it was before you reset it.</p> <p>Here's a <code>UIScrollView</code> subclass that fixes the bug without resetting the scroll view to the top whenever you return from a modal:</p> <pre><code>@interface NonBuggedScrollView : UIScrollView @end @implementation NonBuggedScrollView { CGPoint oldOffset; } -(void)willMoveToWindow:(UIWindow *)newWindow { oldOffset = self.contentOffset; self.contentOffset = CGPointMake(0,0); } -(void)willMoveToSuperview:(UIView *)newSuperview { oldOffset = self.contentOffset; self.contentOffset = CGPointMake(0,0); } -(void)didMoveToWindow { self.contentOffset = oldOffset; } -(void)didMoveToSuperview { self.contentOffset = oldOffset; } @end </code></pre> <p>If you'd rather do this in a <code>UIViewController</code> than in a <code>UIScrollView</code> subclass, change the content offset in the <code>viewWillAppear:</code> and <code>viewDidAppear</code> methods.</p> <p>If you don't want to preserve where the user's scroll position when they return from a modal, and just want to scroll the <code>UIScrollView</code> back to the top, as the OP asked for, then all you need is the even simpler:</p> <pre><code>@interface NonBuggedScrollView : UIScrollView @end @implementation NonBuggedScrollView -(void)willMoveToWindow:(UIWindow *)newWindow { self.contentOffset = CGPointMake(0,0); } -(void)willMoveToSuperview:(UIView *)newSuperview { self.contentOffset = CGPointMake(0,0); } @end </code></pre>
    singulars
    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.
    1. VO
      singulars
      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