iOS: Best way to make a UIScrollView with dynamic height?

后端 未结 2 1931
悲&欢浪女
悲&欢浪女 2021-01-27 00:31

I\'m currently setting up a UIScrollView with the following structure

UIScrollView
--ContentView (UIView)
  --ContainerView1 (UIView)
     --UILabel1
     --UILa         


        
相关标签:
2条回答
  • 2021-01-27 00:37

    //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 讨论(0)
  • 2021-01-27 00:54

    //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)];
    
    0 讨论(0)
提交回复
热议问题