2012-01-14 52 views
0

我需要的UIView动态大小(它可以方含1个或2个标签),我做这样的事情:添加子视图到UIView后,我可以为UIView设置框架吗?

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

当然,实际上只是点击运行而不是登录并提出这个问题会更快? – 2012-01-14 17:27:01

+0

嗯,是的,我知道它的工作原理,但我只是想知道这是否是正确的做法... – 2012-01-14 17:29:46

回答

0

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

相关问题