Iphone – scrollsToTop doesn’t work when UIWebView used with pushViewController, looking for solution

iphoneuiwebview

I have an app with a table view at the root in a navigation controller, and on selection of a table cell it displays a new view controller that contains only a UIWebView (with a toolbar and a navbar).

Depending on how I present the new web view, the feature where the user can tap on the status bar at the top and have the webview scroll to the top, either works or doesn't work.

If I use:

  • (void)presentModalViewController:(UIViewController *)modalViewController animated:(BOOL)animated

on the RootView, then the webview does scroll to the top.

If I change that one line of code and instead use:

  • (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated

on the navigation controller, then the scrollsToTop feature stops working.

What I really want to use however, for other reasons in the context of the app, is the pushViewController method. BUT, I also want to keep the scrollsToTop behaviour.

I have so far tried various approaches, some described here:

-Attempting to set the webview internal scrollView scrollsToTop property

((UIScrollView *)[[webView valueForKey:@"_internal"] valueForKey:@"scroller"]).scrollsToTop = YES;

(No discernible effect).

-Changing the ordering of setting NavBar properties or not setting any at all
-Adding extra "window makeKeyAndOrderFront" calls after the new view push.

I don't believe there are other views there that could be claiming the "scrollsToTop" property (and the first test above proves that in any case).

Short of attempting to embed UIWebView into a UIScrollView, which I expect will be painful, I have run out of routes to explore to resolve this issue.

I am hoping someone else has found a way to correct this?

Best Answer

Try the reverse approach as a workaround.

In the root view controller:

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    self.tableView.scrollsToTop = NO;
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.tableView.scrollsToTop = YES;
}

Bye!

Related Topic