【发布时间】:2015-12-17 10:34:36
【问题描述】:
我有以下情况:
- 带有一个孩子的 UIScrollView ("_scrollView"):UIView
- UIView ("_layoutContainer") 的子视图数量未知
- UIView 的子视图以编程方式添加,其约束以编程方式设置。
添加子视图的代码(以及我尝试调整滚动视图的内容大小):
- (void)viewDidLoad {
[super viewDidLoad];
UIView* lastLabel = _topCollectionView;
for (int i = 0; i<50; i++) {
UILabel* imageLabelView = [UILabel new];
imageLabelView.translatesAutoresizingMaskIntoConstraints = NO;
[_layoutContainer addSubview:imageLabelView];
[_layoutContainer addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(10)-[labelview]-(10)-|" options:0 metrics:nil views:@{@"labelview":imageLabelView}]];
[_layoutContainer addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[previousLabel]-(10)-[imagelabel(40)]" options:0 metrics:nil views:@{@"previousLabel":lastLabel, @"imagelabel":imageLabelView}]];
imageLabelView.text = @"DUMMY";
lastLabel = imageLabelView;
}
[_layoutContainer setNeedsLayout];
[_layoutContainer layoutIfNeeded];
_scrollView.contentSize = _layoutContainer.frame.size;
[_scrollView invalidateIntrinsicContentSize];
}
我的情节提要约束如下所示:
此代码在日志中没有任何约束错误,并且看起来像预期的那样工作。
我的问题是: 滚动视图的内容大小不会改变。我可以添加任意数量的标签,但滚动永远不起作用。 你能帮我动态放大我的滚动视图吗?
【问题讨论】:
标签: ios objective-c uiscrollview autolayout nslayoutconstraint