I'm currently setting up a UIScrollView with the following structure
UIScrollView
--ContentView (UIView)
--ContainerView1 (UIView)
--UILabel1
--UILabel2
--ContainerView2 (UIView)
--UILabel3
--UILabel4
--ContainerView3 (UIView)
--UILabel5
--UILabel6
I've pinned all the four edges for all the elements above and also have defined their height constraints (Storyboard complaints of zero height if I didn't do so).
Now whenever my UILabel receives its text from the server, I call sizeToFit for all the labels. Then I use the lastObject of the containerViews to calculate the supposing new height. Then I call setNeedsLayout. However, nothing changes in my app!
This is the code that I use to calculate the new height and adjust. Perhaps have I programmed it wrong? Sorry my concept on constraints is not strong, may have set the constraints the wrong way too?
- (void) relayoutAllSubViews: (NSArray *) arrayOfContainerViews
{
//Relayout subviews
for (int i = 0; i < [arrayOfContainerViews count]; i++)
{
UIView * indView = [arrayOfContainerViews objectAtIndex:i];
if (indView.hidden == YES)
{
indView.frame = CGRectMake(indView.frame.origin.x, indView.frame.origin.y, indView.frame.size.width, 0);
}
else
{
UIView * lastSubviewInView = [indView.subviews lastObject];
CGFloat subviewEndPos = lastSubviewInView.frame.origin.y + lastSubviewInView.frame.size.height;
if ((subviewEndPos - (indView.frame.origin.y + indView.frame.size.height)) > 0)
{
indView.frame = CGRectMake(indView.frame.origin.x, indView.frame.origin.y, indView.frame.size.width, (subviewEndPos - indView.frame.origin.y));
}
}
}
//Adjust scrollview
UIView * lastView = [self.scrollView.subviews lastObject];
CGFloat newEndPos = lastView.frame.origin.y + lastView.frame.size.height;
self.contentView.bounds = CGRectMake(self.contentView.bounds.origin.x, self.contentView.bounds.origin.y, self.contentView.bounds.size.width, (newEndPos - self.contentView.bounds.origin.y));
self.scrollView.contentSize = self.contentView.bounds.size;
[self.view setNeedsLayout];
}
Need Help!!