【发布时间】:2014-12-29 05:49:49
【问题描述】:
我在UIScrollView 中有一个UIView。我可以轻松地将高度固定到UIScrollView 的框架高度。
如何添加一个固定到 UIScrollView 的 contentSize 的约束?
谢谢!
【问题讨论】:
标签: xcode ios7 uiscrollview autolayout xcode6
我在UIScrollView 中有一个UIView。我可以轻松地将高度固定到UIScrollView 的框架高度。
如何添加一个固定到 UIScrollView 的 contentSize 的约束?
谢谢!
【问题讨论】:
标签: xcode ios7 uiscrollview autolayout xcode6
UIScrollView 有动态约束,left、top、width 和 height 是在运行时生成的。如果您将 UIView 放在 UIScrollview 中并在 Interface Builder 中固定固定约束,则会产生错误,因为参数是相对于 Superview/Container View 的。
您可以尝试一些解决方法:
1- 以编程方式添加 UIView 约束
http://www.thinkandbuild.it/learn-to-love-auto-layout-programmatically/
2- 在 UIView 子类中的 initWithFrame 函数中手动调整视图边界
请给我任何有关您的进度的反馈。
【讨论】:
“以编程方式添加约束”的答案是正确的,但我接受它作为完整答案的细节有点少。
这就是我的做法!
--代码--
- (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];
}
【讨论】: