UITableView被UITabBar部分隐藏

我有一个包含UINavigationControllerUITabBarController 。 在可见的UIViewController ,我正在编程创build一个UITableView ,如下所示:

 self.voucherTableView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame] style:UITableViewStylePlain]; self.voucherTableView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth; 

但是,UITabBar与UITableView重叠。

当我输出[[UIScreen mainScreen] applicationFrame]的高度时,它返回460.00,而它应该是367.00。

在界面生成器中,我使用“模拟度量标准”自动将视图的高度设置为367.00。

有什么我失踪,无论我尝试什么,我不能看到我需要的367.00高度。

作为一个临时解决scheme,我已经手动设置了UITableView的框架,这不是很理想,所以最好解决这个问题:

 self.voucherTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 367) style:UITableViewStylePlain]; 

您应该使用self.view.bounds而不是[[UIScreen mainScreen] applicationFrame]作为最后一个返回给你整个屏幕框架,而self.view.bounds为您提供您的视图边界至于你正在寻找什么。

您应该将UINavigationController实例添加到UITabBarController ,然后将一个表视图控制器添加到UINavigationController实例的rootViewController属性,这应该使您的生活更容易。

作为一个简单的例子,创build一个空的基于窗口的应用程序(这些模板使得它比实际更令人困惑)。

将您的UIViewController/UITableViewController子类添加到项目中,然后使用此代码作为设置项目的指导。 这段代码在你的AppDelegate类中:

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // create our table view controller that will display our store list StoresViewController *storeListController = [[StoresViewController alloc] init]; // create the navigation controller that will hold our store list and detail view controllers and set the store list as the root view controller UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:storeListController]; [navController.tabBarItem setTitle:@"TableView"]; [navController.tabBarItem setImage:[UIImage imageNamed:@"cart.png"]]; // create our browser view controller BrowserViewController *webBrowserController = [[BrowserViewController alloc] init]; [webBrowserController.tabBarItem setTitle:@"WebView"]; [webBrowserController.tabBarItem setImage:[UIImage imageNamed:@"web.png"]]; // add our view controllers to an array, which will retain them NSArray *viewControllers = [NSArray arrayWithObjects:navController, webBrowserController, nil]; // release these since they are now retained [navController release]; [storeListController release]; [webBrowserController release]; // add our array of controllers to the tab bar controller UITabBarController *tabBarController = [[UITabBarController alloc] init]; [tabBarController setViewControllers:viewControllers]; // set the tab bar controller as our root view controller [self.window setRootViewController:tabBarController]; // we can release this now since the window is retaining it [tabBarController release]; [self.window makeKeyAndVisible]; return YES; } 

在上面的代码示例中, BrowserViewControllerUIViewController的子类, StoresViewController类是UITableViewController的子类。 UITabBarControllerUINavigationController实例是以编程方式创build的,并添加到窗口中。

通过UITableViewController类,您可以避免以编程方式创buildUITableView实例,并获得所需的大部分function。

当你需要在UINavigationController实例的堆栈上推送一个详细视图时,你只需要使用类似这样的东西:

 [self.navigationController pushViewController:YourDetailViewControllerInstance animated:YES]; 

这会将详细视图UIViewController子类添加到UINavigationController实例的视图层次结构中,并为该转换添加animation。

这里有很多控制器,但这是完全值得的,并且会避免很多你遇到的问题,因为这个方法允许视图pipe理resize,并把工具栏/导航条全部考虑在内。