繁体   English   中英

WebView动态调整大小到内容大小

[英]WebView dynamic resize to content size

我无法调整WebView元素的大小以适应其内容。 我不想调整内容的大小,我希望WebView及其包含的Custom View和NSWindow调整大小以适应内容。

目前我已尝试使用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

据我所知,你有一个包含自定义视图的窗口,该视图本身包含一个Web视图。

您希望将内容加载到Web视图中,并使Web视图的框架更改大小以适应该内容。 您还希望包含视图和窗口相应地调整大小。

一旦您知道如何根据其内容调整Web视图的大小,如我对其他问题的回答中所述 ,您可以相应地轻松调整容器的大小。

我将在这里重复我的回答,添加调整包含对象的大小。

正如我在其他答案中所指出的那样,你无法提前知道内容有多大,许多网站都太大而无法放在屏幕上。 您可能需要将窗口的高度限制在屏幕高度,以使其有用。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM