2017-10-26 11 views
0

*コメントに記載されている行は、制約にバインドされていないサブビューを親ビューに拡大しています。高さの制約が増え、「ランタイムでサブビューが追加されました」

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

enter image description here

+0

ここで達成しようとしていることは何ですか?あなたがここで尋ねていることは明らかではありません。ピンクの眺めを大きくして、すべてのラベルがその内側にあるようにしますか(3つのラベルより少し上)? – Ladislav

+0

はいピンク色はこれらすべてのタグビューの親ビューです。私はピック1の下に他の親ビューを追加する必要があり、これらのすべての親ビューはスクロールビューになります。最初に私はtableViewを使ってこの全部をやってみました。異なるプロトタイプのセルを作り、行の高さを自動二次元にしました。しかし、セルでも同じことが起こります。セル内の項目の制約が増えると、テーブルビューのセルの更新を開始し終了した後にセルの高さを上げる必要があります。タグの高さも増加します。タイトルとクロスbtnは、タグ内の中央揃えであり、上下の制約はありません。 –

+1

'MSTag * tagItem = [[[NSBundle mainBundle] loadNibNamed:@" MSTag "owner:self options:kNilOptions] objectAtIndex:0];' XIBファイルには自動マスクがあります柔軟な高さなので、スーパービューの高さが変わると、タグの高さは – Ladislav

答えて

1

XIBファイルは柔軟な高さに設定されたマスクを自動サイズ変更しているように見えますので、その高さは、スーパーの高さの変化を変更し、ちょうど

以下

tagItem.autoresizingMask = UIViewAutoresizingNone

を追加

MSTag * tagItem=[[[NSBundle mainBundle] loadNibNamed:@"MSTag" owner:self options:kNilOptions] objectAtIndex:0];

スーパービューの高さが変更されると、正しい自動サイズ変更マスクが設定され、タグの高さが高くならない

関連する問題