I have a UIView
inside a UIScrollView
. I can easily pin the height to the UIScrollView
's frame height.
How do I add a constraint that pins to the UIScrollView's contentSize
instead?
Thanks!
I have a UIView
inside a UIScrollView
. I can easily pin the height to the UIScrollView
's frame height.
How do I add a constraint that pins to the UIScrollView's contentSize
instead?
Thanks!
UIScrollView have dynamic constraints, left, top, width and height are generated at runtime. If you put a UIView inside a UIScrollview and Pin fixed constraints in Interface Builder it will generate an error because the parameters are relative to Superview/Container View.
You can try some workarounds:
1- Add UIView constraints Programmatically
http://www.thinkandbuild.it/learn-to-love-auto-layout-programmatically/
2- Manually resize your view bounds in initWithFrame function inside a UIView Subclass
Please give me any feedback about your progress.
The answer "Adding constraints programatically" is correct but it was a little light on detail for me to accept it as the full answer.
Here's how I did it!
--Code--
- (void)webViewDidFinishLoad:(UIWebView *)webView {
_scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width, _headerImageView.frame.size.height + webView.scrollView.contentSize.height);
_webView.translatesAutoresizingMaskIntoConstraints = NO;
NSDictionary *viewsDictionary = @{@"myWebView":_webView};
NSString *constraintsString = [NSString stringWithFormat:@"V:[myWebView(%i)]", (int)_scrollView.contentSize.height];
NSArray *constraint_H = [NSLayoutConstraint constraintsWithVisualFormat:constraintsString options:0 metrics:nil views:viewsDictionary];
[_webView addConstraints:constraint_H];
}