0

So what I am after is a way to put a UIButton on top of a UIScrollView and a UIWebView under it. The reason I want it this way is that I need the button to "scroll away" as the user scrolls the page I load into the UIWebView.

In order for this to work, I need to get the height of the content of the web page I load and then set the height of the web view to match this. If I can do this, I then intend to set the contentSize of the UIScrollView to match the heights of the button and web view.

I understand that somehow this is supposed to happen in the - (void)webViewDidStartLoad:(UIWebView *)webView method, which is confirmed on many threads on stackoverflow (for example this one). I also know that Apple says you shouldn't put a UiWebView in a UIScrollView, since the scrolling actions may interfere. Disabling scroll for the web view should avoid the problem though.

There are many threads discussing this matter, but none of them seem to work for me. Can it be because I am running on iOS7?



I am in big desperation here, help is much appreciated!

Community
  • 1
  • 1
OscarWyck
  • 2,515
  • 5
  • 21
  • 26

2 Answers2

3

You can get height using following code :

NSString *heightStrig = [webView stringByEvaluatingJavaScriptFromString:@"(document.height !== undefined) ? document.height : document.body.offsetHeight;"];
float height = heightStrig.floatValue;

complete code :

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    CGRect frame = webView.frame;
    NSString *heightStrig = [webView stringByEvaluatingJavaScriptFromString:@"(document.height !== undefined) ? document.height : document.body.offsetHeight;"];
    float height = heightStrig.floatValue + 10.0;
    frame.size.height = height;
    webView.frame = frame;
}
Kushagra
  • 31
  • 5
0

You can get the height of the content loaded into a UIWebView as follows:

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    CGFloat contentHeight = webView.scrollView.contentSize.height;
}

And then adjust the frame of the web view accordingly.

neilco
  • 7,964
  • 2
  • 36
  • 41
  • I have tried this before. But how do you suggest I change the web view frame? In this method? And also in this method change the IOScrollView frame? ... does not seem to work for me. – OscarWyck Oct 11 '13 at 06:53