Objective c UIViewController'的适当尺寸;s观点
在处理了这么多小时而没有任何运气之后,我正在尝试不同的方法,在我的Objective c UIViewController'的适当尺寸;s观点,objective-c,ios,uitableview,uiviewcontroller,Objective C,Ios,Uitableview,Uiviewcontroller,在处理了这么多小时而没有任何运气之后,我正在尝试不同的方法,在我的UIViewController的loadView中以编程方式创建视图。我在我的UIViewController中有UIToolbar和UITableView,但是在将视图添加为另一个UIView的子视图时,我在调整大小方面遇到了问题。以下是我目前得到的信息: 在自定义UIViewController的loadView中: { [super loadView]; //this doesn't seem righ
UIViewController
的loadView中以编程方式创建视图。我在我的UIViewController
中有UIToolbar
和UITableView
,但是在将视图添加为另一个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];
}
{
[super loadView];
controller = [[CustomViewController alloc] init];
[self.customView addSubview:controller.view];
[controller.view setAutoresizesSubviews:TRUE];
}
在rootViewController的loadView()中:
您可以在此处看到屏幕截图:
我是新来的,除了尺码正确之外,我走对了吗
非常感谢
编辑: 我正试图创建一个这样的视图
- UIViewController
- UINavigationBar
- UIView从我可以从你的代码中扣除的内容来看,你想要一个UITableView来填充所有屏幕,除了屏幕上部的一个条。(通常UIToolBar位于屏幕底部)
如果您使用a并将其作为a的rootViewController,您将免费获得许多良好的行为和大小
在
中,屏幕顶部的条形图是UINavigationController
,如果需要,您可以通过此调用在底部设置一个navigationBar
:工具栏
- (void)setToolbarHidden:(BOOL)hidden animated:(BOOL)animated
这是一个值得研究的好地方。您所做的一些假设与预期使用UIViewController的方式相反,我认为不匹配是您困惑的根源 UIViewController通常不管理其自身根视图的大小。其视图的大小由其容器调整。这意味着窗口或容器视图控制器(如UINavigationController,或使用ios5api的自定义容器视图控制器)管理控制器视图的框架 尝试调整控制器的帧是-加载视图不太可能产生效果,因为视图的帧可能会在以后添加到窗口或父控制器的视图时重置。同样,控制器不应在加载视图后调整其自身视图的帧,因为这可能与其父视图的
行为相竞争-layoutSubviews
相反,我认为您需要一个视图控制器,其视图同时包含要显示的UITableView和UINavigation栏。正如@VinceBurn所建议的,听起来您想要的是UINavigationController已经提供的行为。Vince,请参见上面的编辑。我不能使用UINavigationController,因为我需要两个UITableView,每个UITableView都有自己的工具栏。@Eric,好的,从您的编辑中,我看到您希望将UIViewController放置在其他UIViewController中,除非您在iPad上使用UIPlitViewController,否则可能会给您带来麻烦。通常是每个屏幕一个UIViewController。乔纳,谢谢你的回答。如果我以编程方式设置视图,我假设我需要在根视图添加到父控制器的视图之前先设置视图的布局,对吗?如果是这样,我可以在哪里执行此操作?当根视图在
中构造时,您可以布局子视图。您只需通过设置适当的自动调整大小掩码或在自定义UIView子类上实现-loadView
,为该根视图的帧稍后更改做好准备。请注意,如果您在iOS 5之前嵌套UIViewController,或者不使用iOS 5提供的子视图控制器方法,则可能会遇到意外行为。我试图在《谢谢你的建议和分享你的帖子》上对此进行更详细的报道。我刚开始iOS开发,很难理解视图控制器是如何工作的。-layoutSubviews