簡體   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