簡體   English   中英

正確調整UIViewController視圖的大小

[英]Proper sizing of UIViewController's view

與此處理后問題這么多小時,沒有任何運氣,我在我的編程方式創建視圖嘗試不同的方法UIViewController的的loadView。 我的UIViewControllerUIToolbarUITableView ,但是在將視圖添加為另一個UIView的子視圖時,我無法調整大小。 這是到目前為止我得到的:

在我的自定義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和UITableView的自定義UIViewController
    • 右邊的UIView <<,與上面相同

除了我將有兩個UITableView且沒有分段按鈕之外,該控件與我正在嘗試的操作很接近。 https://plus.google.com/u/0/photos/112841433450419935389/albums/5693241484889252817/5693260341279558818

從我可以推斷出的代碼中,您希望UITableView可以填充所有屏幕,但屏幕上方的欄除外。 (通常,UIToolBar位於屏幕底部)

如果您使用UITableViewController並將其作為UINavigationController的rootViewController,則將有很多不錯的行為並免費提供尺寸。

UINavigationController中,屏幕頂部的欄是navigationBar ,如果需要,您可以在此調用的底部具有toolBar

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

iOS的《 View Controller編程指南》是一個不錯的地方。

您所做的一些假設與預期使用UIViewControllers的方式相反,我認為不匹配是造成混淆的根源。

UIViewController通常不管理其自己的根視圖的大小。 它們的視圖通過容器來調整大小。 這意味着窗口或容器視圖控制器(如UINavigationController)(或使用iOS 5 API的自定義容器視圖控制器)將管理控制器視圖的框架。

嘗試調整控制器的框架是-loadView不太可能起作用,因為在以后將視圖添加到窗口或父控制器的視圖時,可能會重置視圖的框架。 同樣,在加載視圖后,控制器不應調整其自身視圖的框架,因為這可能與其父視圖的-layoutSubviews行為競爭。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM