0

The line mentioned with *comments is stretching the subviews that are not bound with any constraint to its parent view.

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

Sushil Sharma
  • 2,321
  • 3
  • 29
  • 49
Muhammad Sarim
  • 401
  • 5
  • 11
  • What are you trying to achieve here? It is not obvious what you are asking here...Do you want the pink view to be as big so that all labels are inside it (a bit more than Three label)? – Ladislav Oct 26 '17 at 13:06
  • yes the pink one is parent view of all these tagViews. I have to add other parent views below the pick one and these all parent views will be in scroll view. First I tried doing this whole thing using tableView and create different prototype cells and put the row height automatic dimention. But, same happens in cell as well, increasing the constraint of item in cell should increase cell height, after calling begin and end updating cell of table view. but it also increasing tag heights. title and cross btn are center align in tags no top and down constraint attach to it. – Muhammad Sarim Oct 26 '17 at 13:38
  • 1
    Try adding `tagItem.autoresizingMask = UIViewAutoresizingNone` below `MSTag * tagItem=[[[NSBundle mainBundle] loadNibNamed:@"MSTag" owner:self options:kNilOptions] objectAtIndex:0];` It looks like XIB file has autoresizing mask to flexible height, so when superview's height changes so to will the tags height – Ladislav Oct 26 '17 at 13:48
  • Yes it work as it should, Why didin't you answer it separately, I want to mark it as accepted answer. – Muhammad Sarim Oct 26 '17 at 13:57
  • I will right now – Ladislav Oct 26 '17 at 13:58

1 Answers1

1

It looks like XIB file has autoresizing mask set to flexible height, and thus its height changes when superview's height changes, just add

tagItem.autoresizingMask = UIViewAutoresizingNone

below

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

this will set the correct autoresizing mask and tags will not grow in height when superview's height changes

Ladislav
  • 7,223
  • 5
  • 27
  • 31