How to determine UIWebView height based on content, within a variable height UITableView?
صفحه اصلي

How to determine UIWebView height based on content, within a variable height UITableView?

I am trying to create a UITableView with variable height rows as explained in the answer to this question

My problem is each cell contains a UIWebView with different (statically loaded) content I can't figure out how to calculate the proper height based on the content. Is there a way to do this? I've tried things like this:

   (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {        WebViewCell *cell = (WebViewCell*)[self tableView:tableView cellForRowAtIndexPath:indexPath];        [cell setNeedsLayout];        [cell layoutIfNeeded];        return cell.bounds.size.height;     } 

The cells themselves are loaded from a nib, which is simply a UITableViewCell containing a UIWebView. (It would also be fine if the cells just adjusted themselves to the largest of the html content, though variable height would be nicer).

How to get a C method to accept UIImage parameter?


how to release uiview added as subview in scrollview?
This code is probably too slow for table view use, although does the trick. Populate a UITableView with dataIt doesn't look like there's any alternative, as UIWebView offers no direct access to the DOM.. Dynamically change url or WordPress theme if UserAgent is iPhone In a view controller's viewDidLoad I load any HTML in a webview and when the load is finished run any javascript to return the element height.. inheritance in objective C
- (void)viewDidLoad {     [super viewDidLoad];     webview.delegate = self;     [webview loadHTMLString:@"<div id='foo' style='background: red'>The quick brown fox jumped over the lazy dog.</div>" baseURL:nil]; }  - (void)webViewDidFinishLoad:(UIWebView *)webView {     NSString *output = [webview stringByEvaluatingJavaScriptFromString:@"document.receive ElementById(\"foo\").offsetHeight;"];     NSLog(@"height: %@", output); } 
Program to change the phone numbers a call is redirected to if a mobile phone cannot be reached?How to change UITabBar Selection color


iPhone SDK: GameKit and large files + connection lost
I was very glad to find your answers, the javascript trick worked well for me.. However, I wanted to say this there is also the "sizeThatFits:" method :.
CGSize goodSize = [webView sizeThatFits:CGSizeMake(anyWidth,anyHeigth)]; 
We have to set a preferred size different from zero, although apart from that, it usually always works !. Usually, for the reason this with the UIWebViews, it seems to job only on the second loading (I use the "loadHTMLString:" method, and yes I call "sizeThatFits:" from the delegate "didFinishLoad:" method). . So, that's why I was very happy to find your quick fix which works in any case..


This seems to work. For now..
- (void)webViewDidFinishLoad:(UIWebView *)webView {     CGFloat webViewHeight = 0.0f;     if (self.subviews.count > 0) {         UIView *scrollerView = [self.subviews objectAtIndex:0];         if (scrollerView.subviews.count > 0) {             UIView *webDocView = scrollerView.subviews.lastObject;             if ([webDocView isKindOfClass:[NSClassFromString(@"UIWebDocumentView") class]])                 webViewHeight = webDocView.frame.size.height;         }     } } 
It should safely return 0 if it fails..


The problem with -sizeThatFits: is this it doesn't job out of the box, at least not on iOS 4.1. It just returns the current size of the webView (no matter whether it's called with CGSizeZero or an arbitrary non-zero size).. I found out this it actually works if you reduce the frame height of the webView prior to calling -sizeThatFits:.. See my solution:


measuring a webview works although only when it has loaded its content.
  • making webview small (like 5px)
  • load it and wait for finish
  • calling sizeToFit on it then
  • receive ting the size THEN
  • reload the tableview (return the correct height for the cell)
advertisement: :D see my M42WebviewTableViewCell class on github which struggles with this.


If you have bad values with sizeThatFits the first time although not after, here a possible cause I've encountered : a bug with external CSS. It seems webViewDidFinisLoad is called to soon, before external CSS are retrieved, and the web document fully built. I think the bug disappears if you reload for the reason this of the cache.. A trick : Preload your CSS with a dummy UIWebView soon in your application.. SDK : IOS4.


The better approach is to use scrollView.contentSize.height property, like below..
- (void)webViewDidFinishLoad:(UIWebView *)webView {      NSLog(@"%f",webView.scrollView.contentSize.height);    } 

70 out of 100 based on 35 user ratings 430 reviews