2012-01-03 86 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

相关问题