简体   繁体   English

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

[英]WebView dynamic resize to content size

I'm having trouble resizing a WebView element to fit its content. 我无法调整WebView元素的大小以适应其内容。 I don't want to resize the content, I want the WebView and its containing Custom View and NSWindow to resize to fit the content. 我不想调整内容的大小,我希望WebView及其包含的Custom View和NSWindow调整大小以适应内容。

At the moment i've tried using JavaScript to get the width and height and resizing the 3 elements, as well as using NSRect and setFrameSize but with no luck (shown below). 目前我已尝试使用JavaScript获取宽度和高度并调整3个元素的大小,以及使用NSRect和setFrameSize但没有运气(如下所示)。

So basically, if I have an NSWindow with a Custom View with a Web View, how do I get them to resize to fit the content of the WebView? 所以基本上,如果我有一个带有Web视图的自定义视图的NSWindow,我如何让它们调整大小以适应WebView的内容?

Thanks in advance everyone! 在此先感谢大家!

This is what I have at the moment which just makes a big mess (it doesn't resize the main window, and makes the contents of the window spill out of it). 这就是我现在所做的一件大事(它没有调整主窗口的大小,并使窗口的内容溢出)。

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

As far as I can tell, you have a window containing a custom view, which itself contains a web view. 据我所知,你有一个包含自定义视图的窗口,该视图本身包含一个Web视图。

You want to load content into the web view and have the frame of the web view change size to accommodate that content. 您希望将内容加载到Web视图中,并使Web视图的框架更改大小以适应该内容。 You also want the containing view and window to resize accordingly. 您还希望包含视图和窗口相应地调整大小。

Once you know how to resize a web view according to its content, as explained in my answer to this other question , you can then easily resize the containers accordingly. 一旦您知道如何根据其内容调整Web视图的大小,如我对其他问题的回答中所述 ,您可以相应地轻松调整容器的大小。

I am going to repeat my answer here, with additions that resize the containing objects. 我将在这里重复我的回答,添加调整包含对象的大小。

As noted in my other answer, you can't know ahead of time how big the content will be, and many web sites will be too big to fit on screen. 正如我在其他答案中所指出的那样,你无法提前知道内容有多大,许多网站都太大而无法放在屏幕上。 You will likely need to constrain the height of your window to the screen height for this to be useful. 您可能需要将窗口的高度限制在屏幕高度,以使其有用。

- (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