2013-06-20 90 views
1

我目前正在使用webView來顯示新聞,下面的消息,我打算把一些buttons.so我需要得到的webView的高度,以改變按鈕的位置後,所有下載的webView的高度。換句話說,如何動態改變webView的高度?如何動態改變webView的高度?

+0

self.webView.frame.size.height爲u想要什麼? – iCoder

回答

4

搜索試試這個

- (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]; 
     } 
    } 
4

做這個委託方法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); 
} 
1

web視圖具有方法sizeThatFits:一旦Webview是在一個視圖層次添加完成加載HTML返回內容的大小。

但是,這不工作沒有警告。在調用配件尺寸方法之前,您需要將webview的框架設置爲較小的值。

對於例如:如果你有一個固定的寬度和高度可變

//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:我想給信貸的原創作品。我仍然在SO

1
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

+0

這不是「動態」,你只是將webView的高度固定爲350,而設備是iPhone 4. – piaChai

+0

是的,另一種方式使其成爲「動態」..自動佈局可用... –