Mahesh M
Mahesh M

Reputation: 85

UiWebview in IPhone resize as per the content size

enter image description hereI am having an issue like i am using UIWebView in Iphone and data coming as HTML am storing the html data as astring and passing to the UIWebView am getting it in good but i want to Change the UIWebView Size as per the content size

    - (void)viewDidLoad {
        web.delegate = self; 
        NSString * str = [dict objectForKey:@"terms"];
        [web loadHTMLString:str baseURL:nil];
    }
- (void)webViewDidStartLoad:(UIWebView *)webView
{
        CGRect frame = web.frame;
        frame.size.height = 1;

        CGSize fittingSize = [web sizeThatFits:CGSizeZero];
        frame.size = fittingSize;
        web.frame = frame;

        NSLog(@"size: %f, %f", fittingSize.width, fittingSize.height);

     web.frame = CGRectMake(10, 100, web.scrollView.contentSize.width, web.scrollView.contentSize.height);

}

Upvotes: 1

Views: 1075

Answers (3)

joern
joern

Reputation: 27620

You could use the delegate method webViewDidFinishLoad: but sometimes this is called before the HTML is fully rendered and that leads to wrong height values.

To make this work reliably you have to wait until the HTML is fully rendered and then use Javascript to send the correct height to your UIWebView.

Please have a look at this blogpost I wrote some time ago.

Upvotes: 1

ryancrunchi
ryancrunchi

Reputation: 475

Do it with your web view's delegate method :

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    webView.frame = CGRectMake(0, 0, webView.scrollView.contentSize.width, webView.scrollView.contentSize.height);
}

Upvotes: 0

Dipen Chudasama
Dipen Chudasama

Reputation: 3093

set your web view delegate and frame.

 yourwebview.frame=[[UIScreen mainScreen] bounds]; 
 yourwebview.delegate = self;

end then use this delegate method to set height.

- (void)webViewDidFinishLoad:(UIWebView *)aWebView {
        CGSize contentSize = aWebView.scrollView.contentSize;
        NSLog(@"webView contentSize: %@", NSStringFromCGSize(contentSize));
        yourwebview.contentsize = contentSize;
    }

Upvotes: 2

Related Questions