1

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 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

What should be equal height? The UIView » UIScrollView? That prevents scrolling, right?
the constraints to scrollview should be aligned top,leading,trailing and bottom w.r.t superview and constraints of child view should be applied aligned top,leading,trailing and bottom w.r.t scrollview and need to give height equal to childview.(for e.g. if its 1200px, it should be fixed)
That is what I am doing right now. The UIView is leading:0, trailing:0, top:0, bottom:0, equalWidth to ScrollView, height:540 (fixed)
what is the height of scrollview ?
The scrollview's height is 800 (Layout class w:compact h:regular)
1

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 Comment

This seems to make the issue worse, please see the screenshot added to my original post.
1

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.