WebView高度未根据其内容进行设置

时间:2015-01-07 05:00:00

标签: ios objective-c iphone

我知道这个问题发布了很多次,但我仍然无法根据内容更改UIWebView的高度

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title=@"webview";
    [_webView setDelegate:self];

    NSString * string = @"<h1>This is HTML string</h1>";
    [_webView loadHTMLString:string baseURL:nil];
}

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    return YES;
}


- (void)webViewDidStartLoad:(UIWebView *)webView
{
    NSLog(@"start loadinng.......");

}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    NSLog(@"finish loading.......");



    CGRect frame = _webView.frame;
    frame.size.height = 1;
    _webView.frame = frame;
    CGSize fittingSize = [_webView sizeThatFits:CGSizeZero];
    frame.size = fittingSize;
    _webView.frame = frame;
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    NSLog(@"Failed to load with error :%@",[error debugDescription]);

}

有人可以帮助我如何调整UIWebView身高吗?

4 个答案:

答案 0 :(得分:0)

这是你的答案。

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    if (webView.isLoading){
        return;
    }
    CGSize contentSize = webView.scrollView.contentSize;
    NSLog (@"height: %f",contentSize.height);
 }

获得此高度后,使用新的宽度和高度重新构建webView,并重新构建其父视图(如果有)。

答案 1 :(得分:0)

这个怎么样。

- (void)webViewDidFinishLoad:(UIWebView *)webView
{

    [webView sizeToFit];
    webView.frame = CGRectMake(x,y,width,webView.frame.size.height);
}

答案 2 :(得分:0)

我通常使用这些方法来设置UIWebview框架的内容大小:

- (void)webViewDidStartLoad:(UIWebView *)webView
{
    CGRect frame = webView.frame;

    frame.size.height = 5.0f;

    webView.frame = frame;
}


- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    CGSize mWebViewTextSize = [webView sizeThatFits:CGSizeMake(1.0f, 1.0f)];  // Pass about any size

    CGRect mWebViewFrame = webView.frame;


    mWebViewFrame.size.height = mWebViewTextSize.height;

    webView.frame = mWebViewFrame;


    //Disable bouncing in webview
    for (id subview in webView.subviews)
    {
        if ([[subview class] isSubclassOfClass: [UIScrollView class]])
        {
            [subview setBounces:NO];
        }
    }
}

当WebView完成加载内容时,会自动调用它们(如果您将webviews委托给此类),


  1. check this

  2. raywenderlich-tutorial

  3. 这可能对您有所帮助:)。

答案 3 :(得分:0)

First calculate height of web view for desired content.
NSString* lString = @"<h1>This is HTML string</h1>";
NSAttributedString* lAttributedText = [[NSAttributedString alloc] initWithData: [lString dataUsingEncoding:NSUTF8StringEncoding]
options:@{ 
NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding)
}                                                               
documentAttributes: nil 
error:nil];

CGRect lBoundingRect = [lAttributedText boundingRectWithSize: CGSizeMake(_webView.bounds.size.width, CGFLOAT_MAX) options: NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading context: nil];

then set it to _webView.frame

Now, load web view
[_webView setDelegate:self];

[_webView loadHTMLString: lString baseURL:nil];