我在UIView中有3个标签,我想将标签位置设置为如下位置: -
Label 1
Label 2
Label 3
现在我用了
label1.frame = CGRectMake(0,0,Width,Height); //for 1st label
label2.frame = CGRectMake(0,label1.frame.size.height,Width,Height);
//for 2nd label which is correct.
label3.frame = CGRectMake(0,label2.frame.size.height,Width,Height);
//But when i set like this, position will not show correctly. Any idea??
答案 0 :(得分:1)
试试这个。 (您必须同时计算按钮height
并设置第3个标签y position = label1 + label2 height
)
UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(0,0,[UIScreen mainScreen].bounds.size.width,60)]; //for 1st label];
UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(0,label1.frame.size.height,[UIScreen mainScreen].bounds.size.width,60)];
UILabel *label3 = [[UILabel alloc] initWithFrame:CGRectMake(0,label1.frame.size.height + label2.frame.size.height,[UIScreen mainScreen].bounds.size.width,60)];
NSLog(@"Label 1 Frame = %@", NSStringFromCGRect(label1.frame));
NSLog(@"Label 2 Frame = %@", NSStringFromCGRect(label2.frame));
NSLog(@"Label 3 Frame = %@", NSStringFromCGRect(label3.frame));
<强>输出强>
标签1帧= {{0,0},{320,60}}
标签2帧= {{0,60},{320,60}}
标签3帧= {{0,120},{320,60}}
答案 1 :(得分:1)
您还应该考虑Y位置
label2.frame = CGRectMake(0,label1.frame.origin.y + label1.frame.size.height,Width,Height);
label3.frame = CGRectMake(0,label2.frame.origin.y + label2.frame.size.height,Width,Height);
建议:使用Stackview