2012-04-24 73 views
1

我無法調整WebView元素的大小以適應其內容。 我不想調整內容大小,我想讓WebView及其包含的自定義視圖和NSWindow調整大小以適應內容。WebView動態調整大小爲內容大小

目前我已經嘗試使用JavaScript獲取寬度和高度,並調整3個元素的大小,以及使用NSRect和setFrameSize但沒有運氣(如下所示)。

所以基本上,如果我有一個具有Web視圖的自定義視圖的NSWindow,我如何讓他們調整大小以適應WebView的內容?

在此先感謝大家!

這是我現在只是弄得一團糟(它不調整主窗口,並使窗口的內容溢出)。

NSString *width = [_myWebView stringByEvaluatingJavaScriptFromString:@"document.body.scrollWidth;"]; 
NSString *height = [_myWebView stringByEvaluatingJavaScriptFromString:@"document.body.scrollHeight;"]; 

NSSize size; 
size.width = [width floatValue]; 
size.height = [height floatValue]; 

NSRect mySize = NSMakeRect(_window.frame.origin.x, _window.frame.origin.y, size.width, size.height); 
[_window setFrame:mySize display:YES]; // Resize main NSWindow 
[_webViewView setFrameSize:size]; // Resize custom view of main NSWindow 
[_myWebView setFrameSize:size]; // Resize WebView in custom view 
+0

的可能重複[如何根據其內容調整的WebView?](http://stackoverflow.com/questions/2675244/how-to-resize-webview-according-to-its-content) – 2012-04-24 08:35:55

+0

你發佈[非常類似的問題](http://stackoverflow.com/q/10288219/50122)11小時前問完全相同的東西。我投票結束這個問題,因爲它是重複的,現在你又發佈了你的問題。我以前告訴過你,如果你的原始問題沒有得到解答,你應該*不要*發佈另一個問題,你應該[開始賞金](http://stackoverflow.com/faq#bounty)。但是,在這種情況下,您的問題已經在我作爲重複鏈接的問題中得到了解答,所以請閱讀該答案。 – 2012-04-24 08:39:20

+0

它根本不是重複的,視圖和它的容器是完全不同的,以及問題,因爲在另一篇文章中我想調整內容的大小,而不是容器。此外,我已經做了很多嘗試,現在有代碼分享以強調我的問題。另外,使用JavaScript獲得寬度和高度的方式完全不同。沒有什麼是完全重複的!所以,請不要跟着我說我所寫的一切都是重複的,所以其他想幫助的人也可以。謝謝。 – Cristian 2012-04-24 08:42:14

回答

3

據我所知,有一個包含自定義視圖,它本身包含一個Web視圖窗口。

您想要將內容加載到Web視圖中,並使Web視圖的框架更改大小以適應該內容。您還希望包含的視圖和窗口相應地調整大小。

一旦您知道如何根據其內容調整Web視圖的大小(如my answer to this other question中所述),則可以相應地輕鬆調整容器的大小。

我打算在這裏重複我的答案,並添加了調整包含對象的大小。

正如我的其他答案所指出的,您無法預先知道內容有多大,而且許多網站會過大而無法在屏幕上顯示。您可能需要將窗口的高度限制在屏幕高度,以便有用。

- (void)loadWebPage 
{ 
    //set ourselves as the frame load delegate so we know when the window loads 
    [webView setFrameLoadDelegate:self]; 

    //turn off scrollbars in the frame 
    [[[webView mainFrame] frameView] setAllowsScrolling:NO]; 

    //load the page 
    NSURLRequest* request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://daringfireball.net"]]; 
    [[webView mainFrame] loadRequest:request]; 
} 

//called when the frame finishes loading 
- (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)webFrame 
{ 
    if([webFrame isEqual:[webView mainFrame]]) 
    { 
     //get the rect for the rendered frame 
     NSRect webFrameRect = [[[webFrame frameView] documentView] frame]; 
     //get the rect of the current webview 
     NSRect webViewRect = [webView frame]; 

     //calculate the difference from the old frame to the new frame 
     CGFloat deltaX = NSWidth(webFrameRect) - NSWidth(webViewRect); 
     CGFloat deltaY = NSHeight(webFrameRect) - NSHeight(webViewRect); 

     //make sure our web view and its superview resize automatically when the 
     //window resizes 
     [webView setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable]; 
     [[webView superview] setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable]; 


     //set the window's frame, adjusted by our deltas 

     //you should probably add some code to constrain the size of the window 
     //to the screen's content size 
     NSRect windowFrame = window.frame; 
     [webView setFrameSize:NSMakeSize(NSWidth(window.frame) + deltaX, NSHeight(window.frame) + deltaY)]; 

     //turn scroll bars back on 
     [[[webView mainFrame] frameView] setAllowsScrolling:YES]; 
    } 
} 
+1

這很酷。最後一行應該是'[[[self.webView mainFrame] frameView] setAllowsScrolling:YES];'當你重新開啓滾動條時! – shmim 2015-05-12 19:52:39

+0

謝謝,現在修復。 – 2015-05-15 05:04:22