2012-01-03 80 views
0

與此issue這麼多小時處理沒有任何的運氣後的正確尺寸,我在我的UIViewController的編程的loadView創建視圖嘗試不同的方法。我有我的UIViewControllerUIToolbarUITableView,但我有麻煩施膠添加視圖作爲另一個UIView的子視圖時。這裏就是我有這麼遠:UIViewController的視圖

在我的自定義的UIViewController」的loadView:

{ 

    [super loadView]; 

    //this doesn't seem right!?!? 
    self.view.frame = CGRectMake(0, 0, 400, 300); 

    //create the Tool Bar 
    self.toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 45.01)]; 

    [self.toolbar setBarStyle:UIBarStyleDefault]; 
    [self.toolbar setAutoresizesSubviews:TRUE]; 
    [self.toolbar setAutoresizingMask:(UIViewAutoresizingFlexibleWidth)]; 

    // create the array to hold the buttons, which then gets added to the toolbar 
    NSMutableArray* buttons = [[NSMutableArray alloc] initWithCapacity:1]; 

    //create buttons 
    UIBarButtonItem* bi = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:nil]; 
    bi.style = UIBarButtonItemStyleBordered; 
    [buttons addObject:bi]; 

    //add buttons to the toolbar 
    [self.toolbar setItems:buttons animated:NO]; 

    //add toolbar to the view 
    [self.view addSubview:self.toolbar]; 


    //create UITableView 
    //a better way setting to set the frame!?!? 
    UITableView* listTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 
                      self.toolbar.bounds.size.height, 
                      self.view.bounds.size.width, 
                      self.view.bounds.size.height - self.toolbar.bounds.size.height) 
                  style:UITableViewStylePlain]; 
    [listTable setAutoresizesSubviews:TRUE]; 
    [listTable setAutoresizingMask:(UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight)]; 
    [listTable setDataSource:self]; 
    [listTable setDelegate:self]; 

    self.table = listTable; 
    [self.view addSubview:listTable]; 
} 

在RootViewController的公司的loadView():

{ 
    [super loadView]; 

    controller = [[CustomViewController alloc] init]; 
    [self.customView addSubview:controller.view]; 
    [controller.view setAutoresizesSubviews:TRUE]; 
} 

您可以在這裏看到的截圖: https://plus.google.com/u/0/photos/112841433450419935389/albums/5693241484889252817/5693241483569881554

我是新本,比獲得正確的大小等,我在正確的軌道上?

非常感謝。


編輯:

我想創建這樣

  • 的UIViewController
    • UINavigationBar的
    • 的UIView < <左邊的圖,UIToolbar的自定義的UIViewController和UITableView
    • 的UIView < <右側,同上

這一個接近我想要不同的是,我將有兩個的UITableView並沒有分割按鈕執行。 https://plus.google.com/u/0/photos/112841433450419935389/albums/5693241484889252817/5693260341279558818

回答

0

從我可以扣除你的代碼你想要一個UITableView來填充屏幕上方的一個欄以外的所有屏幕。 (通常一個UIToolBar是在屏幕的底部)

如果你把一個UITableViewController並把它作爲一個UINavigationController的RootViewController的你將有很多好吃的行爲和上漿是免費的。

UINavigationController在屏幕頂部的酒吧是navigationBar,你可以有一個toolBar在與該呼叫的底部,如果你想:

- (void)setToolbarHidden:(BOOL)hidden animated:(BOOL)animated 

而且View Controller Programming Guide for iOS是看的好地方成。

+0

Vince,請參閱上面的修改。我無法使用UINavigationController,因爲我需要兩個UITableView,並且每個人都有自己的工具欄。 – Eric 2012-01-03 04:50:59

+0

@Eric,好的,從你的編輯中我發現你想把UIViewControllers放在其他的UIViewController中,除非你在使用UISplitViewController的iPad上,這會導致你麻煩。通常它是每個屏幕一個UIViewController。 – 2012-01-03 05:03:50

0

你已經取得了一定的假設是違反預期UIViewControllers使用的方式,我認爲這是不匹配的,在你的困惑的根源。

UIViewControllers通常不會管理自己的根視圖的大小。他們的觀點被它的容器重新調整。這意味着窗口或容器視圖控制器(如UINavigationController)(或使用iOS 5 API的自定義容器視圖控制器)管理控制器視圖的框架。

試圖調整控制器的框架是-loadView不太可能產生影響,因爲視圖的框架可能會在稍後將視圖添加到窗口或父控制器視圖時重置。同樣,控制器在視圖加載後不應該調整它自己的視圖框架,因爲這可能會與其父視圖的行爲相沖突。

相反,我認爲你想要一個視圖控制器的視圖包含您要顯示的UITableView和UINavigation欄。正如@VinceBurn建議,它聽起來像你想要一個UINavigationController已經提供的行爲。

+0

喬納,謝謝你的回答。如果我以編程方式設置視圖,我假設我需要在將根視圖添加到父控制器的視圖之前首先設置視圖的佈局,我是否正確?如果是這樣,我可以在哪裏做到這一點? – Eric 2012-01-04 04:19:26

+0

當在'-loadView'中構建根視圖時,您可以佈局子視圖,您只需要爲根視圖的框架做好準備,稍後可以通過設置適當的自動識別掩碼或在自定義UIView子類上實現'-layoutSubviews'來進行更改。 請注意,如果您在iOS 5之前嵌套UIViewControllers或者不使用iOS 5提供的子視圖控制器方法,您可能會遇到意外的行爲。我試圖在http://blog.carbonfive.com/2011/03/09/abusing-uiviewcontrollers/ – Jonah 2012-01-04 07:11:13

+0

更詳細地介紹這一點,感謝您的建議和分享您的文章。我剛開始使用iOS開發,並且很難理解視圖控制器的工作原理。 – Eric 2012-01-05 04:55:36

相關問題