iOS自动布局与ScrollView:内容问题

时间:2015-09-30 07:55:57

标签: ios objective-c uiscrollview autolayout

我需要什么

我有一个带有以下层次结构的scrollView:

  

滚动型
  。 ^ contentView(UIView)
  。 - ^ view1(黄色)
  。 - ^ view2(灰色)

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

问题在于我对如何设置view2的高度约束感到茫然。 scrollview需要从顶部到底部运行约束才能计算contentSize。但view2的高度只有在添加子视图后才能修复,当然,这将具有确定高度所需的所有限制。

我尝试了什么

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

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,以便在子视图约束启动时,高度约束将被覆盖。但由于某种原因,这似乎并不起作用。

2 个答案:

答案 0 :(得分:3)

您可以为view2提供一个占位符大小,该大小将在运行时自动删除,以使自动布局系统满意

enter image description here

答案 1 :(得分:1)

我建议, 您可以将高度约束添加到view2并将IBOutlet链接到高度约束。

作为

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

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

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

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

相关问题