2010-07-16 51 views
1

我可能沒有正確地做到這一點。在我做這些事情之前,我還沒有做過iPad應用程序。我有一個UIView將由其他UIViews組成。具體而言,我想動態創建子視圖併爲其添加子視圖。換句話說:UIView沒有與setFrame定位

UIView 
    | 
    | --> UIView 
      | 
      | --> Dynamic UIView 1 
      | --> Dynamic UIView 2 
      | --> ... 

我設法增加其initWithFrame這段代碼獲得「動態的UIView 1」加載廈門國際銀行:

NSArray *nibViews = [[NSBundle mainBundle] loadNibNamed:@"ItemView" 
                owner:[[ItemViewController alloc] init] 
               options:nil]; 
ItemView *infoView = [nibViews objectAtIndex:0]; 
[self addSubview:infoView]; 

它的工作原理!我遇到的問題是「動態UIView n」總是呈現在屏幕的左上角。如果我嘗試使用setFrame更改位置,則在表示框架時會出現黑框,但實際控制內容仍保留在左上角。

任何想法?有一個更好的方法嗎?

----- -----更新

這裏是根視圖的屏幕截圖。 Overview of main UIView http://www.freeimagehosting.net/uploads/460090996d.png

整個控件調用CurrentItemsViewController。綠色區域是一個UIView,我通過代碼中的鏈接將代碼中的鏈接返回到代碼示例中,它被稱爲「itemsUIView」。這裏是我如何CurrentItemsViewController.m

- (id)init { 
[super initWithNibName:nil 
       bundle:nil]; 

UITabBarItem *tbi = [self tabBarItem]; 
[tbi setTitle:@"Current"]; 

/* Approach 1 */ 
NSArray *nibViews = [[NSBundle mainBundle] loadNibNamed:@"ItemView" 
                owner:[[ItemViewController alloc] init] 
               options:nil]; 
ItemView *subItemView1 = [nibViews objectAtIndex:0]; 

/* Approach 2 - this apprach does Approach 1 in the initWithFrame method */ 
//ItemView *subItemView1 = [[ItemView alloc] initWithFrame:CGRectMake(120, 120, 354, 229)]; 

/* Approach 3 */ 
//ItemViewController *ctr = [[ItemViewController alloc] init]; 
//UIView *subItemView1 = [ctr view]; 

[[self view] addSubview:subItemView1]; 
// [[self itemsUIView] addSubview:subItemView1]; <-- doesn't render subItemView1 at all 
[subItemView1 release]; 

[subItemView1 setFrame:CGRectMake(120, 120, 354, 229)]; 
[subItemView1 setBounds:CGRectMake(120, 120, 354, 229)]; 

return self; 
} 

中添加項目ItemView.m的裏面我有這樣的,它只能通過方法2和3

- (id)initWithFrame:(CGRect)frame { 
if ((self = [super initWithFrame:frame])) { 

    NSArray *nibViews = [[NSBundle mainBundle] loadNibNamed:@"ItemView" 
                 owner:[[ItemViewController alloc] init] 
                options:nil]; 
    ItemView *infoView = [nibViews objectAtIndex:0]; 
    [self addSubview:infoView]; 

} 
return self; 
} 

獲取通話如圖所示,我嘗試創建ItemView有3種不同的方式,每種都呈現相同的結果。當我添加視圖作爲根視圖的子視圖時,我得到了這個。注意,一個方塊出現在它應該呈現的位置,但是ViewItem實際上在左上角呈現。在上面的示例中,當我添加subItemView1時,它根本不呈現任何東西。

alt text http://www.freeimagehosting.net/uploads/c6635ce860.png

+1

請提供一些更多的細節:截圖,initWithFrame的'整個代碼:','的代碼setFrame' ... – 2010-07-16 09:45:38

+0

設我知道你是否想要更多。 – Steven 2010-07-16 19:03:30

回答

1

嘗試將幀添加它作爲一個子視圖之前。

您的代碼:

NSArray *nibViews = [[NSBundle mainBundle] loadNibNamed:@"ItemView" 
               owner:[[ItemViewController alloc] init] 
              options:nil]; 
ItemView *infoView = [nibViews objectAtIndex:0]; 
self addSubview:infoView]; 

應該是:

NSArray *nibViews = [[NSBundle mainBundle] loadNibNamed:@"ItemView" 
               owner:[[ItemViewController alloc] init] 
              options:nil]; 
ItemView *infoView = [nibViews objectAtIndex:0]; 
infoView.frame = CGRectMake(put your rect here); 
[self addSubview:infoView];