17

I have had to change a UIScrollView into a UIWebView due to formatting reasons.

There is a problem though that I need to be able to get the content offset back to 0, 0 (as I am using reusable UIWebViews and changing content) and get the content size so I can place a button at the bottom; but UIWebView does not seem to have this.

Jesse Beder
  • 33,081
  • 21
  • 109
  • 146
jodm
  • 2,607
  • 3
  • 25
  • 40

5 Answers5

28

This problem can be solved quite easily:

[[webView scrollView] setContentInset:UIEdgeInsetsMake(64, 0, 44, 0)];

Remember that UIEdgeInset is different from CGRect!
A guide on UIEdgeInset can be found in the iOS Developer Library.

BaviDaan
  • 291
  • 3
  • 5
8

The best way I found to sort the problem was:

  1. Create a UIWebView
  2. Put it in a UIScrollView
  3. Add the HTML into it using "loadHTMLString"
  4. Using the "- (void)webViewDidFinishLoad:(UIWebView *)webView {" delegate method I detect the size of the UIWebView

  5. Set the frame of the UIWebView to the content size.

  6. Remove interaction from the UIWebView
  7. Set the Content size of the UIScrollView to the content size.

Then used the functionality of the UIScrollView instead of the UIWebView.

Thanks -JM

Paras Joshi
  • 20,427
  • 11
  • 57
  • 70
jodm
  • 2,607
  • 3
  • 25
  • 40
  • 2
    what you doing in the 5th and 7th steps? Maybe, some code example? Whhich content size? – marko Jan 21 '11 at 10:34
  • 4
    I think that this is a really bad idea since you'll end up creating a UIWebView that will exhaust all your resources - creating a UIWebView that is thousands of pixels high is bad news. – Lee Sep 27 '13 at 08:53
7

Use JavaScript.

I think that the next line should do the magic:

[webView stringByEvaluatingJavaScriptFromString:@"window.scroll(0,0)"];
Michael Kessler
  • 14,245
  • 13
  • 50
  • 64
  • Ill give that a try. What about getting content height so i can add a button at the bottom of the UIWebView? – jodm Aug 19 '10 at 19:37
  • Hmmm. This one is more complicated. I believe it is possible. I suppose that you have to take in account the height of the webView, the height of the HTML (by using JavaScript) and the current zoom level. Or, you might disable the zoom (if not done so yet) and then it will be much simpler. Just locate your button so that its bottom will be few pixels above the height of the webView. you should locate it above the HTML by using style/CSS (google for "css floating div"). – Michael Kessler Aug 19 '10 at 21:00
  • By the way, you might locate an Objective-C button (UIButton) above the web view. This way you you won't need to learn CSS or JavaScript at all... – Michael Kessler Aug 19 '10 at 21:02
  • See the solution I did above. Seems to be working just got a few tweaks and then should be fine... :o) Cheers for your help. – jodm Aug 19 '10 at 21:28
  • I was facing a similar problem and definitely using javascript script is the best option you have! – Mateus Jun 09 '13 at 06:34
3

Now you can ask for the contentSize property of the scrollView property of a UIWebView. This enables you to also change the content size of that web view, using CGSizeMake (width, height).

ekinnear
  • 437
  • 2
  • 10
1

Once you get the UIScrollView that is inside the UIWebView, it will be easy to change ContentOffset and contentSize. Information here on how to get the scrollView : ScrollOffset in UIWebView?

Community
  • 1
  • 1
CedricSoubrie
  • 6,657
  • 2
  • 39
  • 44