简体   繁体   中英

WebView height not set based on its content

I know this question was posted so many times but still I can not change the height of UIWebView based on its content

- (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]);

}

can anybody help me how can resize my UIWebView height?

Here is your answer.

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

After gettting this height reframe your webView with new width and height and also reframe its parent view if there any.

How about this.

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

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

I usually use these methods, to set UIWebview frame as it's content size:

- (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];
        }
    }
}

They are automatically called (if you set webviews delegate to this class), when WebView has finished loading it's content.

or

  1. check this

  2. raywenderlich-tutorial

This might helps you :)

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];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM