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

Sarp Kaya
  • 3,686
  • 21
  • 64
  • 103
  • Can you post some pictures to illustrate what you mean? I'm having a hard time visualizing the problem. – Ian Hoar Aug 02 '13 at 06:01
  • possible duplicate of [UIScrollview Autolayout Issue](http://stackoverflow.com/questions/12580434/uiscrollview-autolayout-issue) – Steph Sharp Aug 02 '13 at 09:02

2 Answers2

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.

Community
  • 1
  • 1
Sarp Kaya
  • 3,686
  • 21
  • 64
  • 103
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.

Community
  • 1
  • 1
Steph Sharp
  • 11,462
  • 5
  • 44
  • 81
  • @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. – Steph Sharp Aug 02 '13 at 07:26