将子视图添加到UIView后,我可以为UIView设置框架吗?

时间:2012-01-14 17:24:30

标签: objective-c uiview size frame

我需要动态大小的UIView(它可以包含1或2个标签),我这样做:

float nextY = 0.0f;
float labelHeight = 20.0f;

UIView *tempView = [[UIView alloc] initWithFrame:CGRectZero];

UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, nextY, 100.0f, labelHeight)];
label1.text = @"Test 1";
[tempView addSubview:label1];
nextY += labelHeight;    

if (something == YES)
{
    UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, nextY, 100.0f, labelHeight)];
    label1.text = @"Test 2";
    [tempView addSubview:label2];
    nextY += labelHeight;
}

tempView.frame = CGRectMake(0.0f, 0.0f, 100.0.f, nextY);

我还好吗?

1 个答案:

答案 0 :(得分:0)

你在做什么是有效的。如果在子视图上设置自动调整遮罩,则在更改超视图的帧时,系统还将调整子视图的帧。