Ios 增加高度约束,拉伸“;“运行时添加子视图”;

Ios 增加高度约束,拉伸“;“运行时添加子视图”;,ios,objective-c,nslayoutconstraint,Ios,Objective C,Nslayoutconstraint,用*comments提到的行将不受任何约束的子视图拉伸到其父视图 NSMutableArray* tagItemLabels=[NSMutableArray new]; NSMutableArray* data = [NSMutableArray arrayWithObjects:@"One",@"One Hundred",@"One Hundred Eleven",@"Thousand One Hundered",@"two",@"Three", nil]; for (NSS

用*comments提到的行将不受任何约束的子视图拉伸到其父视图

NSMutableArray* tagItemLabels=[NSMutableArray new];
    NSMutableArray* data = [NSMutableArray arrayWithObjects:@"One",@"One Hundred",@"One Hundred Eleven",@"Thousand One Hundered",@"two",@"Three", nil];

    for (NSString* title in data) {

        MSTag * tagItem=[[[NSBundle mainBundle] loadNibNamed:@"MSTag" owner:self options:kNilOptions] objectAtIndex:0];
        [tagItem.lblTitle setText:title];
        [self.tagView addSubview:tagItem];

        CGRect frame = tagItem.frame;
        //frame.size.height=30;

        CGSize expectedLabelSize = [title sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14]}];

        if (tagItemLabels.count<=0) {
            frame.origin.x=8;
            frame.origin.y=8;
            frame.size.width=expectedLabelSize.width+70;
            frame.size.height=30;
        }else{
            frame=((UIView*)([tagItemLabels lastObject])).frame;
            frame.origin.x+=frame.size.width+12;
            frame.size.height=30;
            frame.size.width=expectedLabelSize.width+70;
            NSLog(@"%f==%f",frame.origin.x+tagItem.frame.size.width+12,self.tagView.frame.size.width);

            if (frame.origin.x+frame.size.width>self.tagView.frame.size.width) {
                //if (frame.origin.x+frame.size.width>self.TagView.frame.size.width) {
                frame.origin.x=8;
                frame.origin.y+=frame.size.height+8;
            }
        }

        //***If I write this line my views get streched!***//
        self.constraintTagViewHeight.constant=frame.origin.y+frame.size.height;

        [tagItem setFrame:frame];
        [tagItemLabels addObject:tagItem];
    }
NSMutableArray*tagItemLabels=[NSMutableArray new];
NSMUTABLEARRY*数据=[NSMUTABLEARRY数组,其对象为:@“一”,“一百”,“一百一十一”,“一千一百”,“两”,“三”,零];
for(NSString*数据中的标题){
MSTag*tagItem=[[NSBundle mainBundle]loadNibNamed:@“MSTag”所有者:自选项:针织物]对象索引:0];
[tagItem.lblTitle setText:title];
[self.tagView addSubview:tagItem];
CGRect frame=tagItem.frame;
//框架尺寸高度=30;
CGSize expectedLabelSize=[title sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14]}];
if(tagItemLabels.countself.tagView.frame.size.width){
//if(frame.origin.x+frame.size.width>self.TagView.frame.size.width){
frame.origin.x=8;
frame.origin.y+=frame.size.height+8;
}
}
//***如果我写这句话,我的观点就会被扭曲***//
self.constraintTagViewHeight.constant=frame.origin.y+frame.size.height;
[tagItem setFrame:frame];
[tagItemLabels-addObject:tagItem];
}

看起来XIB文件的自动调整大小掩码设置为“灵活高度”,因此当superview的高度更改时,其高度也会更改,只需添加

tagItem.autoresizingMask=uiviewAutoresizingOne

下面

MSTag*tagItem=[[NSBundle mainBundle]loadNibNamed:@“MSTag”所有者:自选项:编织物]对象索引:0]


这将设置正确的自动调整大小遮罩,并且当superview的高度发生变化时,标签的高度不会增加

您在这里想要实现什么?你在这里问的问题并不明显……你想让粉色视图大到所有标签都在里面吗(比三个标签多一点)?是的,粉色视图是所有这些标记视图的父视图。我必须在“拾取一个”下面添加其他父视图,这些所有父视图都将处于滚动视图中。首先,我尝试使用tableView完成整个工作,创建不同的原型单元,并将行高自动标注。但是,同样的情况也发生在单元格中,在调用表视图的开始和结束更新单元格之后,增加单元格中项的约束应该增加单元格高度。但它也增加了标签的高度。标题和交叉btn在标记中居中对齐,没有附加上上下约束。请尝试将
tagItem.autoresizingMask=UIViewAutoresizingNone
添加到
MSTag*tagItem=[[NSBundle mainBundle]loadNibNamed:@“MSTag”所有者:自选项:kNilOptions]objectAtIndex:0]看起来XIB文件具有灵活高度的自动调整大小掩码,因此当superview的高度发生变化时,标签高度是否会改变是的,它会正常工作,为什么不单独回答,我想将其标记为已接受的答案。我现在就回答