How to change the height of webView dynamically?

前端 未结 4 1124
無奈伤痛
無奈伤痛 2021-01-16 10:22

I\'m currently using webView to display news,below the news I intend to put some buttons.so I need to get the height of webView so as to change the position of buttons accor

相关标签:
4条回答
  • 2021-01-16 10:54
    According to your device type like iPhone 4 or iPhone 5 you can set your webview
    The web view has finished loading.
        - (void)webViewDidFinishLoad:(UIWebView *)webView {
    
            [DejalActivityView removeView];
    
            if ([objSpineCustomProtocol windowHeight] == 480) {
    
                termsWebView.frame = CGRectMake(termsWebView.frame.origin.x
                                                ,termsWebView.frame.origin.y
                                                ,termsWebView.frame.size.width
                                                ,350);
    
            }
    
        }// End of Method webViewDidFinishLoad
    

    //VKJ

    0 讨论(0)
  • 2021-01-16 11:00

    Do this inside the delegate method webViewDidFinishLoad:

    - (void)webViewDidFinishLoad:(UIWebView *)aWebView
    {
        CGFloat height = [[aWebView stringByEvaluatingJavaScriptFromString:@"document.height"] floatValue];
        CGFloat width = [[aWebView stringByEvaluatingJavaScriptFromString:@"document.width"] floatValue];
        aWebView.frame = CGRectMake(aWebView.frame.origin.x,aWebView.frame.origin.y,width, height);
    }
    
    0 讨论(0)
  • 2021-01-16 11:03

    try this

        - (void)webViewDidFinishLoad:(UIWebView *)webView
        {
            NSString *string = [_webView stringByEvaluatingJavaScriptFromString:@"document.getElementById(\"body\").offsetHeight;"];
            CGFloat height = [string floatValue] + 8;
            CGRect frame = [_webView frame];
            frame.size.height = height;
            [_webView setFrame:frame];
    
            if ([[self delegate] respondsToSelector:@selector(webViewController:webViewDidResize:)])
            {
                [[self delegate] webViewController:self webViewDidResize:frame.size];
            }
        }
    
    0 讨论(0)
  • 2021-01-16 11:03

    The webView has method sizeThatFits: which returns the size of content once the webView is added in a view hierarchy and finished loading the html.

    But this does not work without a caveat. You need to set the frame of webview to a low value before calling the fitting size method.

    For eg: If you have a fixed width and variable height

    //Set a very low height
    CGRect webViewFrame = self.webView.frame;
    webViewFrame.size.height = 5.0f;
    self.webView.frame = webViewFrame;
    //Re calculate the size that fits
    CGSize size = [self.webView sizeThatFits:webViewFrame.size];
    self.webViewFrame.size = size;
    self.webView.frame = webViewFrame;
    

    PS: I want to give credit to the original work. I'm still searching in SO

    0 讨论(0)
提交回复
热议问题