7

I'm using a UIScrollView for paging three different UIViewControllers. The pager initializes to display page 1 from start. So the user can swipe left or right from the beginning. When I present a modal view controller from the mid view controller, the UIScrollView temporarily scrolls to the first page during the animation of the presented view controller. When I dismiss the modal view controller everything's back to normal and the UIScrollView displays the center view controller.

I would expect (and want) the UIScrollView to keep it's position during the animation.

This is not a technical bug that causes a crash or something, it's just ugly.

Anyone ever had that before? Any idea how to fix it?

flohei
  • 5,248
  • 10
  • 36
  • 61
  • so to be clear, the problem is that your scroll view is scrolling back to your first page when presenting a modal transition from the 2nd page? also, are you using parent-child view controller containment? – Patrick Goley Jul 15 '13 at 23:38
  • can you please try to disable paging on the uiscrollview and check if it still occurs? – Antonio E. Jul 16 '13 at 09:55
  • Did you try setting the property scrollEnabled to NO & then back to YES ? – Woodstock Jul 16 '13 at 10:52
  • Yes, correct. And yes, we're using view controller containment. – flohei Jul 16 '13 at 15:05
  • Setting `scrollEnabled` to no right before loading the modal view does not help. Same for `setPagingEnabled`. It's just weird because when dismissing the modal view controller during the animation I can see the wanted second view. Not the first one that it's animating to during the show animation. – flohei Jul 16 '13 at 15:11
  • That certainly is weird, I'm using pretty much the exact same architecture in my app and i'm not seeing this issue, can you show your source code? – Woodstock Jul 23 '13 at 07:16
  • We fixed it. This behavior was caused by an overwritten `viewWillDisappear` in our super class. For some reason (don't know why yet) someone reset the frame manually in there. – flohei Jul 23 '13 at 09:56
  • Glad to see you fixed the issue! – Woodstock Jul 25 '13 at 13:01
  • Your fix was remove the `[super viewWillDisappear:animated];` line from your `viewWillDisappear` method?, because not work for me – jose920405 Jul 28 '16 at 13:39

1 Answers1

0

I've had this same problem, and after much investigation it appears to be a bug in UIKit relating to scrollviews and AutoLayout. Here's the 'fix'...

In viewDidDisappear:, save the current scrollview contentOffset to a property, and reset it to zero:

- (void)viewDidDisappear:(BOOL)animated 
{
    [super viewDidDisappear:animated];

    self.previousContentOffset = self.scrollView.contentOffset;

    self.scrollView.contentOffset = CGPointZero;
}

Then, in viewWillAppear:, reset the content offset back to what it was previously. I had to dispatch this onto the main queue to get it to work correctly:

- (void)viewWillAppear:(BOOL)animated 
{
    if (!CGPointEqualToPoint(self.previousContentOffset, CGPointZero))
    {
        dispatch_async(dispatch_get_main_queue(), ^{
            self.scrollView.contentOffset = self.previousContentOffset;
        });
    }
}
Vizllx
  • 9,135
  • 1
  • 41
  • 79
  • in `viewDidDisappear` and in `viewWillDisappear` too, the `scrollview.contentOffset` is `{0, 0}`. And `self.scrollView.contentOffset = manual offset;` is not execute, only works if add delay. – jose920405 Jul 28 '16 at 14:11