* 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];
}
答案 0 :(得分:1)
看起来XIB文件的自动调整遮罩设置为灵活高度,因此当superview的高度发生变化时其高度会发生变化,只需添加
tagItem.autoresizingMask = UIViewAutoresizingNone
下面
MSTag * tagItem=[[[NSBundle mainBundle] loadNibNamed:@"MSTag" owner:self options:kNilOptions] objectAtIndex:0];
这将设置正确的自动调整遮罩,当superview的高度改变时,标签的高度不会增加