2012-04-24 2 views
1

내용에 맞게 WebView 요소의 크기를 조정하는 데 문제가 있습니다. 콘텐트의 크기를 조정하고 싶지는 않습니다. WebView와 해당 사용자 지정보기 및 NSWindow가 들어있어 콘텐트에 맞게 크기를 조정하고 싶습니다.WebView 동적 크기를 콘텐츠 크기로 조정

현재 자바 스크립트를 사용하여 너비와 높이를 가져오고 세 요소의 크기를 조정하고 NSRect와 setFrameSize를 사용했지만 행운은 없었습니다 (아래 참조).

기본적으로 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

가능한 중복 [그 내용에 따라 웹보기의 크기를 조정하는 방법?] (http://stackoverflow.com/questions/2675244/how-to-resize-webview-according-to-its-content) –

+0

당신은 11 시간 전에 정확히 똑같은 질문을하는 [매우 비슷한 질문] (http://stackoverflow.com/q/10288219/50122)을 게시했습니다. 그 질문이 중복되어 이제 질문을 다시 게시했기 때문에 투표를 끝내겠다고 표결했습니다. 나는 원래 질문에 답을 얻지 못하면 다른 질문을 게시하지 말아야한다고 말했고 [현상금 시작] (http://stackoverflow.com/faq#bounty)을해야합니다. 그러나이 경우 귀하의 질문에 이미 중복 된 질문으로 답변되어 있으므로 그 답변을 읽으십시오. –

+0

그것은 복제본이 아닙니다.보기와 해당 컨테이너는 완전히 다릅니다. 문제는 물론, 컨테이너가 아니라 내용의 크기를 조정하려는 다른 게시물과 같습니다. 게다가 나는 이것을하기위한 많은 시도를했으며 지금 나의 문제를 강조하기 위해 공유 할 코드를 가지고있다. 또한 JavaScript를 사용하여 너비와 높이를 얻는 방법은 완전히 다릅니다. 전혀 중복이 전혀 없습니다! 그러니 제발, 제게 쓰는 모든 것이 중복되어 도움을 청할 수 있다고 말하면서 저를 따라 다니지 마십시오. 감사. – Cristian

답변

3

내가 알 수있는 한, 사용자 정의보기가 포함 된 창에 웹보기가 있습니다.

콘텐츠를 웹보기에로드하고 해당 콘텐츠를 수용 할 수 있도록 웹보기의 프레임 크기를 변경하려고합니다. 또한 포함 된보기 및 창의 크기를 적절하게 조정해야합니다.

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

+0

고마워, 지금 고쳐 줬어. –

관련 문제