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



我需要具有动态大小的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);

我还好吗?

您所做的是有效的。如果在子视图上设置了自动调整遮罩,那么当更改超视图的框架时,系统也会调整子视图的框架。

最新更新