Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic UIView height with auto layout in iOS 6

For the last couple of days I am trying to accomplish a fairly simple (at least it should be) layout using auto layout constraints, but without success.

My view hierarchy is:
UIScrollView
-- UIView (Container view)
-----UILabel (Multirow label)
-----UIWebView
-----UILabel
-----UIButton

View hierarchy in IB

The desired functionallity is the Container view to be expanding according to the size of the contents. In order to increase the height of the UIWebView I use the following code:

- (void)webViewDidFinishLoad:(UIWebView *)webView {     [self.webView sizeToFit]; }  

I tried many different constraints, with the most reasonable ones being:
1. Pin top space from superview for the 1st label (Event Title)
2. Pin vertical spacing for the 1st label and the UIWebView
3. Pin vertical spacing for the rest elements (i.e. UIWebView - UILabel (Event Date) and UILabel (Event Date) - UIButton)
4. The container view has low priority (1) bottom vertical space constraint with its superview

The result I get is the following: enter image description here

The UIWebView expands but does not push down the event date label and the button, plus the Container view does not expand either.

Any help would be greatly appreciated.

You may download the sample Xcode project from here.

like image 363
Gerhat Avatar asked Jul 07 '13 07:07

Gerhat


2 Answers

You are setting the height contraint of your webView as 266. That's why the height of the web view is still fixed.

You can create this height constraint as an IBOutlet, for example:

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *webViewHeightConstraint; 

And then you can modify the constant of the height constraint when the web view has finished downloading the content. The web view itself consists of scroll view inside, so if you want to get the overall height of the content:

- (void)webViewDidFinishLoad:(UIWebView *)webView {     self.webViewHeightConstraint.constant = self.webView.scrollView.contentSize.height; } 

Or apparently this one also works:

- (void)webViewDidFinishLoad:(UIWebView *)webView {     [self.webView sizeToFit];     self.webViewHeightConstraint.constant = self.webView.frame.size.height; } 
like image 187
Valent Richie Avatar answered Oct 02 '22 16:10

Valent Richie


You might need a delay before updating webview content size. And masonry is useful for autolayout usage.

-(void)webViewDidFinishLoad:(UIWebView *)webview{     [self performSelector:@selector(checkContentOnDelay:) withObject:webview afterDelay:0.001]; }  -(void)checkContentOnDelay:(UIWebView *)webview{     CGSize contentSize = webview.scrollView.contentSize;     if (contentSize.height > 2) {         [webview updateConstraints:^(MASConstraintMaker *make) {             make.height.equalTo(contentSize.height);         }];     }else{         [self performSelector:@selector(checkContentOnDelay:) withObject:webview afterDelay:0.01];     } } 
like image 42
ymutlu Avatar answered Oct 02 '22 16:10

ymutlu