Search code examples
iosuielementcollection

How to change origin of elements based on another elements in ios


Sorry guys one simple doubt but i could not find any answer. My problem is i want to change the origin of element based on another elements. To be more correct for example if i have three labels, i set each labels in same x axis but different y axis one by one. if first label content increases second and third label's y axis should automatically increase. so how i can calculate it..

 UILabel *label1 =[[ UILabel alloc]init];
label1.frame=CGRectMake(10, 50, 10, 150);
label1.text=@"Favorite Cuisine ";

[ViewProfileResultSrollview addSubview: label1];

[label1 sizeToFit];
label1.textColor=[UIColor grayColor];

UILabel *label2 =[[ UILabel alloc]init];
label2.frame=CGRectMake(10, 80, 10, 150);
label2.text=@"Preferred Dress Style";

[ViewProfileResultSrollview addSubview: label2];

[label2 sizeToFit];
label2.textColor=[UIColor grayColor];

UILabel *label3 =[[ UILabel alloc]init];
label3.frame=CGRectMake(10, 120, 10, 150);
label3.text=@"Spoken Languages";

[ViewProfileResultSrollview addSubview: label3];

[label3 sizeToFit];
label3.textColor=[UIColor grayColor];

Solution

  • You could use the previous frame, since you are using [label1 sizeToFit];

    Example below for label 2:

    UILabel *label2 =[[ UILabel alloc]init];
    label2.frame=CGRectMake(10, label1.frame.origin.y+label1.frame.size.height+30, 10, 150);
    label2.text=@"Preferred Dress Style";