Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dismissModalViewControllerAnimated resets contentOffset

I have a problem with my table view. When dismissing a modal view controller presented on top of it, it always scrolling to the top . I have tried observing the changes to contentOffset using KVO, but the one that messes my view goes behind it.

From the UITableViewController, when user finishes his task in the modal dialog, self.tableView.contentOffset is , I call:

[self dismissModalViewControllerAnimated:YES]

Subsequently, when the viewWillAppear:(BOOL)animated is called, the self.tableView.contentOffset is already set to 0,0.

Is this supposed to be happening? I am able to work around the issue by remembering the scroll position before presenting the modal view and restore it back in viewWillAppear after dismissing the modal view. But it seems wrong. Am I missing something?

I have found similar problem described in Dismiss modal view changes underlying UIScrollView.

like image 347
Palimondo Avatar asked Sep 18 '11 08:09

Palimondo


2 Answers

It looks like this is default behavior of UITableViewController. I tested it in very simple app and It worked exactly as you said. If you don't like it, use UIViewController instead.

like image 151
Split Avatar answered Nov 03 '22 00:11

Split


Here is how I work around this problem, so that the table view maintains the original scroll position. In my subclass of UITableViewController I have added:

@property (assign) CGPoint lastScrollPosition;

Then in the implementation, I have overridden the following:

- (void)viewWillAppear:(BOOL)animated
{
    self.tableView.contentOffset = self.lastScrollPosition;
}

- (void)dismissModalViewControllerAnimated:(BOOL)animated
{
    self.lastScrollPosition = self.tableView.contentOffset;
    [super dismissModalViewControllerAnimated:animated];
}

If you want your table to initially appear scrolled to non-zero position, as I did, don't forget to initialize the lastScrollPosition in your viewDidLoad.

like image 28
Palimondo Avatar answered Nov 03 '22 00:11

Palimondo