如何以编程方式增加具有约束的UIScrollView的高度?

时间:2017-02-02 02:05:05

标签: ios objective-c uiscrollview constraints

我是这个iOS应用程序世界的新手,我正在开发一个需要显示项目详细信息的应用程序。这个细节可以收到用户的评论,我需要的是,每当细节得到一个新的评论,我的屏幕高度增加,这里我做了,这是屏幕:

I need to load the comments below the title

These are how my elements are

Scroll的约束是:其超级视图的尾随,前导,顶部和底部。 对于查看内容:其超级视图的尾随,前导,顶部和底部,将中心与x和y对齐,并将高度和宽度与超视图对齐。

所有评论都来自服务,我希望内容增加。我显示所有注释,但UIScrollView不起作用。我认为是因为约束

以下是我如何加载和打印评论并更改我的滚动视图的内容:

    int y  = bottomLine.frame.origin.y + 8;
    int numComments = [[content objectForKey:@"comments"] intValue];

    if (numComments > 0) {
        for (int j=0; j < [comments count]; j++) {

                commentData = [comments objectAtIndex:j];

                //Create all the elements
                viewComments = [[UIView alloc] initWithFrame:CGRectMake(8, y, self.view.bounds.size.width - 16, 109)];
                viewComments.backgroundColor = [UIColor grayColor];

                UIImageView *viewImgComment = [[UIImageView alloc] initWithFrame:CGRectMake(8, 8, 50, 50)];

                NSURL *linckImage = [NSURL URLWithString:[commentData objectForKey:@"image"]];
                [viewImgComment sd_setImageWithURL:linckImage placeholderImage:[UIImage imageNamed:@"place_holder_user"]];
                viewImgComment.layer.cornerRadius = 25;
                viewImgComment.clipsToBounds = YES;

                UILabel *lblTituloComment = [[UILabel alloc] initWithFrame:CGRectMake(50 + 16, 8, (viewComments.bounds.size.width - 74) , 17)];
                [lblTituloComment setText:[commentData objectForKey:@"name"]];
                [lblTituloComment setFont:[UIFont systemFontOfSize:14.0f]];

                UIView *viewRating = [[UIView alloc] initWithFrame:CGRectMake(50 + 16, 17 + 16, 121, 23)];
                viewRating.backgroundColor =[UIColor blackColor];
                viewRating.layer.cornerRadius = 11;

                UILabel *lblDescripcionComment = [[UILabel alloc] initWithFrame:CGRectMake(8, 50 + 8, (viewComments.bounds.size.width - 16), 43)];

                [lblDescripcionComment setText:[commentData objectForKey:@"comment"]];
                [lblDescripcionComment setFont:[UIFont systemFontOfSize:14.0f]];
                [lblDescripcionComment setNumberOfLines:0];

                y = y + 117;

                [viewComments addSubview:viewImgComment];
                [viewComments addSubview:lblTituloComment];
                [viewComments addSubview:viewRating];
                [viewComments addSubview:lblDescripcionComment];

                [masterScroll addSubview:viewComments];
            }
        }
        //Increase the content size
        masterScroll.contentSize = CGSizeMake(self.view.bounds.size.width, y);

我希望有些人可以帮助我,我已经尝试了3天,研究和询问。任何问题或任何解释都让我知道。

1 个答案:

答案 0 :(得分:0)

首先,您必须获得标签高度(标签的内容文本)。

- (CGFloat)getLabelHeight:(UILabel*)label
{
    //Here label is your Comment label.
    CGSize constraint = CGSizeMake(label.frame.size.width, CGFLOAT_MAX);
    CGSize size;

    NSStringDrawingContext *context = [[NSStringDrawingContext alloc] init];
    CGSize boundingBox = [label.text boundingRectWithSize:constraint
                                                  options:NSStringDrawingUsesLineFragmentOrigin
                                               attributes:@{NSFontAttributeName:label.font}
                                                  context:context].size;

    size = CGSizeMake(ceil(boundingBox.width), ceil(boundingBox.height));
    return size.height;
}

获取评论标签高度并设置scrollview内容大小:

CGSize *size;
CGFloat height =  [self getLabelHeight:lblDescripcionComment];
NSLog(@"totalHeight :%f",[self getLabelHeight:lblDescripcionComment]);
lblDescripcionComment.frame = CGRectMake(18, 260, 220, height);

#Here h is your scrollview height without comment label.
[scrollView setContentSize:CGSizeMake(scrollView.frame.size.width,h+height)];