views:

68

answers:

1

I have a UIWebView in which the user can browse a bunch of locally stored and interlinked HTML files. All works nice and well, except in this particular case:

  • User navigates to a page with a fragment pointing to chapter 3, causing (correctly) the browser to render the HTML from the fragment anchor and onward.
  • User scrolls a bit further down, say to chapter 4.
  • User rotates the device to landscape.
  • The UIWebView now jumps back up and shows chapter 3.

So my question is: Is there a way to make UIWebView display the users last location in the document, even though the user scrolls and/or rotates the device during browsing?

A: 

I imagine the reason for that is, the scroll stays at the same pixel position, instead of proportional to the page height. So, when the user has the device in landscape, the page width increases and the page height decreases.

You may want to use JavaScript to scroll the page proportionally when orientation changes, i.e. use [webView stringByEvaluatingJavaScriptFromString:]. Here's the pseudo code:

Just before orientation change:

  1. int oldVerticalPosition = vertical scroll position form the top of the page
  2. int olePageHeight = the height of the whole page

After orientation change:

  1. newPageHeight = the height of the whole page
  2. Scroll the page to newPageHeight * (oldVerticalPosition / oldPageHeight)

This page gives some sample code.

William
Good thinking! This turned out to work like a charm! In order to get the verticalPosition right before rotate I had to set oldVerticalPosition and oldPageHeight in shouldAutorotateToInterfaceOrientation.
thomax
glad it worked! :) I just re-read my post and saw a couple of mistakes... Good that you still got it working.
William