iOS:制作具有动态高度的UIScrollView的最佳方法?

时间:2015-02-03 04:17:40

标签: ios iphone uiscrollview interface-builder xcode-storyboard

我目前正在设置具有以下结构的UIScrollView

UIScrollView
--ContentView (UIView)
  --ContainerView1 (UIView)
     --UILabel1
     --UILabel2
  --ContainerView2 (UIView)
     --UILabel3
     --UILabel4
  --ContainerView3 (UIView) 
     --UILabel5
     --UILabel6

我已经为上面的所有元素固定了所有四个边缘,并且还定义了它们的高度限制(如果我不这样做,则会出现零高度的故事板投诉)。

现在,只要我的UILabel从服务器收到文本,我就会为所有标签调用sizeToFit。然后我使用containerViews的lastObject来计算假设的新高度。然后我调用setNeedsLayout。但是,我的应用程序没有任何变化!

这是我用来计算新高度和调整的代码。也许我编程错了?对不起,我对约束的概念不强,可能也设置了错误的约束方式?

- (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];
}

需要帮助!!

2 个答案:

答案 0 :(得分:2)

//使用以下代码设置滚动视图的内容大小。

    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

答案 1 :(得分:0)

//我添加了动态添加子视图高度的代码

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)];