1
votes

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!!

2

2 Answers

2
votes

//use the below code to set the content size of a scroll view.

    float newHeight=0.0f;
        for(UIView *subViews in [scrollViewObj subviews])
        {
            if([subViews isKindOfClass:[UIView class]])
            {
                newHeight=newHeight+(subViews.frame.origin.x+subViews.frame.size.height);
            }
        }
        NSLog(@"%f",newHeight);
        [scrollView setContentSize:CGSizeMake(300, newHeight)];//change the width as you need
0
votes

//i have added code for dynamically adding the subview heights

float newHeight=0.0f;
    for(UIView *subViews in [scrollViewObj subviews])
    {
        if([subViews isKindOfClass:[UIView class]])
        {
            if([[subViews subviews] count]>0)
            {
                float heightOfSubviews=0.0f;
                for(UIView *insideSubViews in [subViews subviews])
                {
                    heightOfSubviews=heightOfSubviews+(insideSubViews.frame.origin.y+insideSubViews.frame.size.height);
                }
                NSLog(@"%f",heightOfSubviews);
                [subViews setFrame:CGRectMake(subViews.frame.origin.x, subViews.frame.origin.y, subViews.frame.size.width, heightOfSubviews)];
            }

        }
    }
    UIView *viewLastObj=scrollViewObj.subviews.lastObject;
    newHeight=viewLastObj.frame.origin.y+viewLastObj.frame.size.height;
    NSLog(@"%f",newHeight);
    [scrollViewObj setContentSize:CGSizeMake(300, newHeight)];