2012-04-24 75 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