2011-02-13 72 views
0

Screen Shot of what im not trying to do動態創建相似類型的子視圖

我對上述觀點的代碼是:

-(void)viewWillAppear:(BOOL)animated{ 


    float yh = 0; 
    while (yh<200) { 

     //UIView 
     CGRect myFrame = CGRectMake(0, yh, 320, 30); 
     UIView *myFirstView = [[UIView alloc] initWithFrame:myFrame]; 
     myFirstView.backgroundColor = [UIColor orangeColor]; 


     //IUILabel in UIView 
     CGRect mylblFrame = CGRectMake(5, yh, 60, 15); 
     UILabel *lblsize = [[UILabel alloc] initWithFrame:mylblFrame]; 
     lblsize.text = @"Hello"; 
     [myFirstView addSubview:lblsize]; 

     CGRect mylbl_hi = CGRectMake(80, yh, 60, 15); 
     UILabel *lbl_hi = [[UILabel alloc] initWithFrame:mylbl_hi]; 
     lbl_hi.text = @"Hii"; 
     [myFirstView addSubview:lbl_hi]; 


     [self.view addSubview:myFirstView]; 
     [lbl_hi release]; 
     [lblsize release]; 
     [myFirstView release]; 


     yh=yh+40; 


} 
[super viewWillAppear:YES]; 
    } 

我不明白它是這樣的原因......我想的標籤與附着我橙色的子視圖......這可能是奇怪的一天,我明白我的代碼有什麼問題......如果你們中的任何人都可以告訴我,我做錯事的地方對我來說會很好。這是我第一次以編程方式創建視圖..所以請原諒,如果這一切都是愚蠢的問題

回答

0

每個視圖裏面左上角的座標是0,0。

如果您將標籤添加到添加了橙色條的相同視圖中,您的代碼將按預期工作。

你的代碼應該工作,當你在你的標籤改爲y時改爲0.

+0

thanks..appreciated !! – AKG 2011-02-13 23:46:17