【问题标题】:iOS autolayout vs ScrollView : Contentsize issueiOS自动布局与滚动视图:内容大小问题
【发布时间】:2023-03-04 11:06:01
【问题描述】:

我需要什么

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

滚动视图
. ^ 内容视图(UIView)
. - ^ view1(黄色)
. - ^ view2(灰色)

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

问题是我不知道如何设置view2的高度约束。为了计算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 添加一个具有较低优先级的高度约束,以便当子视图约束启动时,高度约束将被覆盖。但由于某种原因,这似乎不起作用。

【问题讨论】:

    标签: ios objective-c uiscrollview autolayout


    【解决方案1】:

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

    【讨论】:

    • 哇!像魅力一样工作!谢谢,兄弟。还有一件事 - 一开始它不起作用,但后来我添加了detailsView.translatesAutoresizingMaskIntoConstraints = NO;,它起作用了。我会添加并接受。再次感谢。
    • 对不起,我忘了,自动布局系统会自动将视图的自动调整掩码转换为约束,所以你应该手动将其设置为 no
    【解决方案2】:

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

    作为

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

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

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

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

    【讨论】:

      猜你喜欢
      • 2013-06-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-25
      • 2013-12-25
      相关资源
      最近更新 更多