简体   繁体   中英

iOS autolayout vs ScrollView : Contentsize issue

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

在此处输入图片说明

I suggest, you to add height constraint to the view2 and link an IBOutlet to the height constraint.

As

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

Then, just before you programmatically add constraints for subview of view2, remove the height constraint from view2, using

 [view2 removeConstraint:self.heightConstraint];

and then add your constraints programmatically.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM