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

Ios 如何以编程方式增加具有约束的UIScrollView的高度?,ios,objective-c,uiscrollview,constraints,Ios,Objective C,Uiscrollview,Constraints,我是iOS应用程序的新手,我正在开发一个需要显示项目详细信息的应用程序。此详细信息可以接收用户的评论,我需要的是,每当该详细信息获得新评论时,我的屏幕高度都会增加,这里是我所做的,这是屏幕: 滚动的约束条件是:拖尾、前导、顶部和底部到其superview。 对于视图内容:尾随、前导、顶部和底部到其超级视图,在x和y方向对齐中心,并与超级视图的高度和宽度相等。 所有的评论都来自一个服务,我想增加内容。我显示了所有的注释,但是UIScrollView不起作用。我想是因为这些限制 下面是我如何加载

我是iOS应用程序的新手,我正在开发一个需要显示项目详细信息的应用程序。此详细信息可以接收用户的评论,我需要的是,每当该详细信息获得新评论时,我的屏幕高度都会增加,这里是我所做的,这是屏幕:

滚动的约束条件是:拖尾、前导、顶部和底部到其superview。 对于视图内容:尾随、前导、顶部和底部到其超级视图,在x和y方向对齐中心,并与超级视图的高度和宽度相等。

所有的评论都来自一个服务,我想增加内容。我显示了所有的注释,但是UIScrollView不起作用。我想是因为这些限制

下面是我如何加载和打印注释以及更改scrollview的内容:

    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);
inty=bottomLine.frame.origin.y+8;
int numComments=[[content objectForKey:@“comments”]intValue];
如果(numComments>0){
对于(int j=0;j<[注释计数];j++){
commentData=[comments objectAtIndex:j];
//创建所有元素
viewComments=[[UIView alloc]initWithFrame:CGRectMake(8,y,self.view.bounds.size.width-16109)];
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占位符图像:[UIImage ImageName:@“place_holder_user”];
viewImgComment.layer.cornerRadius=25;
VIEWIMGCOMENT.clipsToBounds=是;
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*lbldescriptioncomment=[[UILabel alloc]initWithFrame:CGRectMake(8,50+8,(viewComments.bounds.size.width-16),43)];
[lbldescriptioncomment setText:[commentData objectForKey:@“comment”];
[LBLDescriptionComment setFont:[UIFont systemFontOfSize:14.0f]];
[LBLDescriptionComment setNumberOfLines:0];
y=y+117;
[viewComments添加子视图:viewImgComment];
[视图注释添加子视图:lblTituloComment];
[viewComments addSubview:viewRating];
[viewComments添加子视图:LBLDescriptionComment];
[masterScroll addSubview:viewComments];
}
}
//增加内容大小
masterScroll.contentSize=CGSizeMake(self.view.bounds.size.width,y);

我希望有人能帮助我,我已经试了三天,研究和询问。任何问题或解释请告诉我。

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

获取注释标签高度并设置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)];

您能告诉我,对于注释,您使用的是表视图还是uiview?如果表视图没有问题。如果您使用的是uiview,则必须进行一些更改。这不是什么大问题。请告诉我。当然,我正在使用uiview。在添加注释之前保留当前高度的记录。添加注释后,计算视图的高度。查找上一个高度和当前高度之间的差异。2.转到故事板->选择滚动视图的高度约束->为该约束创建出口。->现在转到代码->heghtConstraint.constant=differenceHeight+currentHeightOfScrollView。
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)];