I have a fairly simple view configuration:
A UIViewController
, with a child UIScrollView
and a UIImageView
in this UIScrollView
.
I set the UIImageView
with a height sufficient to break out of the visible area (ie. higher to 1024pt
), and set the Bottom space to superview
constraint of my UIImageView
to a fixed positive value (20
for example).
The whole setup works as expected, the image scrolls nicely in its parent.
Except when the view is scrolled (the effect is more visible if you scrolled to the bottom of the view), then disappear, and appear again (you switched to another view and came back) the scrolling value is restored, but the content of the scroll view is moved to the outside top part of its parent view.
This is not simple to explain, I'll try to draw it:
If you want to test/view the source (or the storyboard, I did not edit a single line of code). I put a little demo on my github: https://github.com/guillaume-algis/iOSAutoLayoutScrollView
I did read the iOS 6 changelog and the explanation on this particular topic, and think this is the correct implementation of the second option (pure auto layout), but in this case why is the UIScrollView
behaving so erratically ? Am I missing something ?
EDIT: This is the exact same issue as #12580434 uiscrollview-autolayout-issue. The answers are just workarounds, as anyone found a proper way to fix this or is this a iOS bug ?
EDIT 2: I found another workaround, which keep the scroll position in the same state the user left it (this is an improvement over 12580434's accepted answer):
@interface GAViewController ()
@property CGPoint tempContentOffset;
@end
@implementation GAViewController
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
self.tempContentOffset = self.mainScrollView.contentOffset;
self.scrollView.contentOffset = CGPointZero;
}
-(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
self.scrollView.contentOffset = self.tempContentOffset;
}
This basically save the offset in viewWillAppear
, reset it to the origin, and then restore the value in viewDidAppear
. The problem seems to occur between these two calls, but I can't find its origin.
question from:
https://stackoverflow.com/questions/15345522/uiscrollview-wrong-offset-with-auto-layout 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…