I have a problem with autolayout(maybe) and my scrollview!
My Problem
This isn't great but I beat auto-layout (definitely not the correct way but I was sick of trying!) by setting the content size in viewDidAppear after autolayout happens, setting the scrollOffset and persisting the scroll offset in viewDidDisappear, and then setting the scroll offset back to it's persisted state in viewDidAppear.
Like this:
-(void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:YES];
self.scrollView.contentSize = self.scrollViewInnerView.frame.size;
self.scrollView.contentOffset = [self.scrollOffsetToPersist CGPointValue];
}
-(void)viewDidDisappear:(BOOL)animated{
[super viewDidDisappear:YES];
self.scrollOffsetToPersist = [NSValue valueWithCGPoint:self.scrollView.contentOffset];
self.scrollView.contentOffset = CGPointZero;
}
Not at all elegant, but works so thought I'd share.
The following code snippet in the containing view controller also seems to solve the problem, without relying on explicit sizes:
- (void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
self.mainScrollView.contentOffset = CGPointZero;
}
It does reset the content offset to the origin, but it seems that so do the other answers.
try this
@property (nonatomic, assign) CGPoint scrollViewContentOffsetChange;
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
self.scrollView.contentOffset = CGPointZero;
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
self.scrollViewContentOffsetChange = _scrollView.contentOffset;
}
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
_scrollView.contentOffset = self.scrollViewContentOffsetChange;
}