2012-01-14 56 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

你在做什麼是有效的。如果您在子視圖上設置了自動調整屏蔽,那麼當您更改超級視圖的框架時,系統也會調整子視圖的框架。

相關問題