I\'m currently using webView to display news,below the news I intend to put some buttons.so I need to get the height of webView so as to change the position of buttons accor
According to your device type like iPhone 4 or iPhone 5 you can set your webview
The web view has finished loading.
- (void)webViewDidFinishLoad:(UIWebView *)webView {
[DejalActivityView removeView];
if ([objSpineCustomProtocol windowHeight] == 480) {
termsWebView.frame = CGRectMake(termsWebView.frame.origin.x
,termsWebView.frame.origin.y
,termsWebView.frame.size.width
,350);
}
}// End of Method webViewDidFinishLoad
//VKJ
Do this inside the delegate method webViewDidFinishLoad:
- (void)webViewDidFinishLoad:(UIWebView *)aWebView
{
CGFloat height = [[aWebView stringByEvaluatingJavaScriptFromString:@"document.height"] floatValue];
CGFloat width = [[aWebView stringByEvaluatingJavaScriptFromString:@"document.width"] floatValue];
aWebView.frame = CGRectMake(aWebView.frame.origin.x,aWebView.frame.origin.y,width, height);
}
try this
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
NSString *string = [_webView stringByEvaluatingJavaScriptFromString:@"document.getElementById(\"body\").offsetHeight;"];
CGFloat height = [string floatValue] + 8;
CGRect frame = [_webView frame];
frame.size.height = height;
[_webView setFrame:frame];
if ([[self delegate] respondsToSelector:@selector(webViewController:webViewDidResize:)])
{
[[self delegate] webViewController:self webViewDidResize:frame.size];
}
}
The webView has method sizeThatFits:
which returns the size of content once the webView is added in a view hierarchy and finished loading the html.
But this does not work without a caveat. You need to set the frame of webview to a low value before calling the fitting size method.
For eg: If you have a fixed width and variable height
//Set a very low height
CGRect webViewFrame = self.webView.frame;
webViewFrame.size.height = 5.0f;
self.webView.frame = webViewFrame;
//Re calculate the size that fits
CGSize size = [self.webView sizeThatFits:webViewFrame.size];
self.webViewFrame.size = size;
self.webView.frame = webViewFrame;
PS: I want to give credit to the original work. I'm still searching in SO