1
votes

I'm running into an issue with autolayout and cannot seem to find an answer to something that should be simple to achieve.

I have the following view hierarchy:

-UIScrollview
  -UIView
     - UILabel
     - UILabel
     - UILabel

The leading/trailing constraints on the labels makes them taller on thinner devices (iPhone 4s vs iPhone 6).

In order to get the UIScrollview to work properly, I need to set the height constraint of the UIView inside of the UIScrollView to avoid an 'ambiguous height' warning.

But when running on an iPhone 4s, the UIView is not tall enough to hold its subviews.

My only solution so far is to create a reference to the height constraint on the UIView and update the height constraint when the view appears:

-(void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
    self.contentHeight.constant =self.lastLabel.frame.origin.y+ self.lastLabel.frame.size.height;
    [self.view layoutIfNeeded];
}

I would think there has to be a better way to do something that is so simple.

I've attached an image to show the issue in detail.

Thank you for your time.

enter image description here

Per @Katty below, making the width/height equal to the scrollview's superview results in bigger issues:

enter image description here

3

3 Answers

1
votes

The view hierarchy you are using is right.Basically what you need to do is in your child view(which is inside Scrollview) you need to align auto layout w.r.t. top,bottom,leading and trailing of scrollview and need to give Equal Height to your child view, which basically calculate the content size for scrollview.

1
votes

Set Trailing Leading Top Bottom With respect to UIScrollView and Height and With either Fix of Set with the Proposition ratio with main view

First Set Equal Height and width then set Proposition multiplier. Hop that image help you to understand what i am try to explain you.

Here is the image that will help to understand

1
votes

After reviewing with another developer I discovered the issue. The bottom subview in the container needs a bottom constraint. That fixed the issue.

Thank you to everyone who took time to help resolve the issue.

enter image description here