iOS autolayout vs UIScrollView: Problem Summary

advertisements

What I Need

I have a scrollView with the following hierarchy :

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

view1(Yellow) has a fixed height and is pinned to the top of the contentView. I have specified all the constraints except for the height view2. coz I am adding a subview to view2(gray) programmatically and will be of random height.

The problem is that I am at a loss on how to set the height constraint of view2. scrollview needs to have constraints running from top through bottom in order to calculate the contentSize. But the height of view2 will only be fixed after the subview is added, which will have all the necessary constraints for determining the height, of course.

What I tried

1) My first plan was to add the subview and set its constraints programmatically to make the scrollview happy. like so :

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]];

The problem is that xcode gives me an error that ScrollView has ambiguous scrollable content height. I cannot give a fixed height for view2 because the subview that I am later adding will have all the necessary constraints to set the ScrollView's `contentSize.

2) Then I tried adding a height constraint to view2 with a lower priority so that when the subviews constraints kick in, the height constraint would get overriden. but that doesn't seem to work for some reason.


You can give view2 a placeholder size which would be removed at runtime automatically to make auto layout system happy