scrollsToTop doesn't work when UIWebView used with pushViewController, looking for solution

StackOverflow https://stackoverflow.com/questions/1808063

  •  05-07-2019
  •  | 
  •  

Question

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?

Was it helpful?

Solution 2

It turns out that it wasn't working because of the way I was customiing the Navbar. I was overriding leftBarButtonItem with a UIBarButtonItem that then took up the whole navbar (I think I took that approach from some Apple code many months ago, but not sure). I was using the same approach on another view and the scrollsToTop worked fine there.

Taking that code out and putting the custom Navbar view into (eg)self.navigationController.titleView instead solved this problem. I do not really know why, like I say it works fine for other views.

I wasted a lot of time with this problem, so I hope I save someone else some hair by describing it here :)

OTHER TIPS

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!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top