iOS 自动布局与滚动视图:内容大小问题

iOS autolayout vs ScrollView : Contentsize issue

我需要什么

我有一个具有以下层次结构的滚动视图:

ScrollView
. ^ contentView(UIView)
. - ^ view1(yellow)
. - ^ view2(gray)

view1(黄色)具有固定的高度并固定在 contentView 的顶部。我已经指定了除高度 view2 之外的所有约束。因为我正在以编程方式向 view2(gray) 添加一个子视图,并且高度是随机的。

问题是我不知道如何设置view2的高度限制。 scrollview 需要从上到下有约束 运行 才能计算 contentSize。但是view2的高度只有在添加子视图后才会固定,当然这将具有确定高度的所有必要约束。

我试过的

1) 我的第一个计划是添加子视图并以编程方式设置其约束以使滚动视图满意。像这样:

detailsView = [ProfileDetailsView instantiateFromNib];
[self.detailHolder addSubview:detailsView];

[self.detailHolder addConstraint:[NSLayoutConstraint constraintWithItem:detailsView
                                                      attribute:NSLayoutAttributeTop
                                                      relatedBy:NSLayoutRelationEqual
                                                         toItem:self.detailHolder
                                                      attribute:NSLayoutAttributeTop
                                                     multiplier:1.0
                                                       constant:0.0]];

[self.detailHolder addConstraint:[NSLayoutConstraint constraintWithItem:detailsView
                                                      attribute:NSLayoutAttributeLeading
                                                      relatedBy:NSLayoutRelationEqual
                                                         toItem:self.detailHolder
                                                      attribute:NSLayoutAttributeLeading
                                                     multiplier:1.0
                                                       constant:0.0]];

[self.detailHolder addConstraint:[NSLayoutConstraint constraintWithItem:detailsView
                                                      attribute:NSLayoutAttributeBottom
                                                      relatedBy:NSLayoutRelationEqual
                                                         toItem:self.detailHolder
                                                      attribute:NSLayoutAttributeBottom
                                                     multiplier:1.0
                                                       constant:0.0]];

[self.detailHolder addConstraint:[NSLayoutConstraint constraintWithItem:detailsView
                                                      attribute:NSLayoutAttributeTrailing
                                                      relatedBy:NSLayoutRelationEqual
                                                         toItem:self.detailHolder
                                                      attribute:NSLayoutAttributeTrailing
                                                     multiplier:1.0
                                                       constant:0.0]];

问题是 xcode 给我一个 ScrollView has ambiguous scrollable content height 的错误。我无法为 view2 提供固定高度,因为我稍后添加的子视图将具有设置 ScrollView 的 `contentSize.

的所有必要约束

2) 然后我尝试向 view2 添加一个优先级较低的高度约束,这样当子视图约束启动时,高度约束就会被覆盖。但由于某种原因,这似乎不起作用。

您可以给 view2 一个占位符大小,它会在运行时自动删除以使自动布局系统满意

我建议, 您向 view2 添加高度约束,并向高度约束添加 link IBOutlet。

作为

 @property (strong, nonatomic) IBOutlet NSLayoutConstraint *heightConstraint;

然后,就在您以编程方式为 view2 的子视图添加约束之前,使用

从 view2 中删除高度约束
 [view2 removeConstraint:self.heightConstraint];

然后以编程方式添加约束。