UIScrollView does not restore properly
Asked Answered
M

2

1

I have a Scrollview, it's properties are set in viewDidAppear. Now when I get to the Scrollview first time there isn't any problem. However I have buttons that are assigned to UINavigationController. So when I press into one of them UINavigationController opens up, when I close the navigation controller, ScrollView does not restore properly. It basically aligns the centre of the screen as previously pressed button location. So if I try to scroll up it does not.

I have tried using this in my viewDidAppear:

scrollView.center = CGPointMake(scrollView.contentOffset.x, scrollView.contentOffset.y); 

Which did not quite work. How can I solve this? I am using iOS6.1

Marashio answered 2/8, 2013 at 2:25 Comment(2)
Can you post some pictures to illustrate what you mean? I'm having a hard time visualizing the problem.Grout
possible duplicate of UIScrollview Autolayout IssueRidgley
M
5

Actually I found the answer here:

UIScrollview Autolayout Issue

The exact code that I used is:

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    //save the current offset
    previousPoint = scrollView.contentOffset;
    //set current view to the beginning point
    self.scrollView.contentOffset = CGPointZero;
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    //retrieve the previous offset
    self.scrollView.contentOffset = previousPoint;
}

previousPoint is nothing but a CGPoint variable declared on the implementation.

Marashio answered 2/8, 2013 at 7:25 Comment(0)
R
2

I've had this problem before too. This answer shows how to overcome this issue.

Basically, you need to set the scrollview's contentOffset appropriately in viewWillAppear: and viewDidDisappear:.

EDIT: Here's another related question that you might find useful, UIScrollview Autolayout Issue.

Ridgley answered 2/8, 2013 at 6:49 Comment(1)
@SarpKaya recentContentOffest is a variable that saves the contentOffset of your scrollview when you leave the view (in your case, when you tap the button the UINavigationController opens up). The answer that I linked to saves this contentOffset when the scrollview disappears so it can be reset when the scrollview appears again.Ridgley

© 2022 - 2024 — McMap. All rights reserved.