1

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?

Kanan Vora
  • 224
  • 1
  • 9
Bandish Dave
  • 791
  • 1
  • 7
  • 27
  • Why you need to increase the WebView height, it can scroll the content if it is bigger than its height... – Kanan Vora Jan 07 '15 at 05:02
  • You haven't done anything to change the height of webview. – iphonic Jan 07 '15 at 05:06
  • @KananVora : thank you for replaying....because i have tableview and each row contain webview and the response of my service is in html string so need to set both cell and webview height based on its content...please help me its very urgent – Bandish Dave Jan 07 '15 at 05:07
  • @iphonic : is there any solution?? how can i set webview height in tableview's row?? – Bandish Dave Jan 07 '15 at 05:11
  • You can get the height of webview content using this `webview.scrollView.contentSize.height;` in `webViewDidFinishLoad` delegate method.. – iphonic Jan 07 '15 at 05:15
  • @iphonic : i had already tried it but still not able to set webview height. even i followed this link http://stackoverflow.com/questions/3936041/how-to-determine-the-content-size-of-a-uiwebview/3937599#3937599 – Bandish Dave Jan 07 '15 at 05:19
  • @BandishDave : Can't you do it like you just show 1 or 2 lines of your content in the UITableViewCell and on its click, show the full content in the next view. Bcoz the whole content will become very big in terms of a cell. – Kanan Vora Jan 07 '15 at 05:43
  • @BandishDave The link you posted http://stackoverflow.com/questions/3936041/how-to-determine-the-content-size-of-a-uiwebview/3937599#3937599 the solution works very fine. See this http://i.imgur.com/JRQq1ko.png – iphonic Jan 07 '15 at 06:00
  • @iphonic : sir same code i do but don't understand where am going wrong. i just drag webview in my view controll in story board and give its size 0,0,320,150. – Bandish Dave Jan 07 '15 at 06:04

4 Answers4

0

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.

Pratik Patel
  • 1,393
  • 12
  • 18
  • i tried this but it returns content size is 504.00 while there is only one line string..so according to my opinion it will be under 50 to 60.can you tell me how can i handle??? – Bandish Dave Jan 07 '15 at 05:40
  • remove [webView sizeToFit] statement first. And also check what size you have given to webView. You should give it minimum size, and then check with one line code. Then also add some more text and then check, it is showing in required height or not. – Pratik Patel Jan 07 '15 at 05:47
  • i set webview from storyboard in my viewcontroll with the size of 0,0,320,150 default. – Bandish Dave Jan 07 '15 at 05:53
  • just try with some more data in html. – Pratik Patel Jan 07 '15 at 06:01
0

How about this.

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

    [webView sizeToFit];
    webView.frame = CGRectMake(x,y,width,webView.frame.size.height);
}
Fahim Parkar
  • 30,974
  • 45
  • 160
  • 276
0

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 :)

Community
  • 1
  • 1
Yuyutsu
  • 2,509
  • 22
  • 38
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];