UIScrollview Autolayout Issue UIScrollview Autolayout Issue ios ios

UIScrollview Autolayout Issue

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.

if you are still searching for an answer i found it today after two days of headbanging the wall. I will just paste you the code, but the most important thing is when you load your scrollView..

    -(void)viewWillAppear:(BOOL)animated{    [scrollView setFrame:CGRectMake(0, 0, 320, 800)];}-(void)viewDidAppear:(BOOL)animated{    [scrollView setScrollEnabled:YES];    [scrollView setContentSize:CGSizeMake(320, 800)];}

all this is loaded before -(void)viewDidLoad

notice the height is in both instances 800, which is crucial for resolving this problem. good luck with your project ;)

I was using adam's solution, but started to have problems when i was dismissing with animated:YES. In my code, content offset gets set a while after viewWillAppear (as viewWillAppear appears to be too soon).

- (void)viewDidDisappear:(BOOL)animated{    self.scrollOffsetToPersist = self.scrollView.contentOffset;    self.scrollView.contentOffset = CGPointZero;    [super viewDidDisappear:animated];}- (void)viewWillAppear:(BOOL)animated{    [super viewWillAppear:animated];    [[NSOperationQueue mainQueue] addOperationWithBlock:^     {         self.scrollView.contentOffset = self.scrollOffsetToPersist;     }];}

EDIT: another, better way is to reset it back in viewDidLayoutSubviews :)

- (void)viewDidLayoutSubviews{    [super viewDidLayoutSubviews];    if(!CGPointEqualToPoint(CGPointZero, self.scrollOffsetToPersist))    {        self.scrollView.contentOffset = self.scrollOffsetToPersist;        self.scrollOffsetToPersist = CGPointZero;    }}