【问题标题】:UITableView partially hidden by UITabBarUITableView 被 UITabBar 部分隐藏
【发布时间】:2011-11-17 09:29:17
【问题描述】:

我有一个UITabBarController,其中包含一个UINavigationController。在可见的UIViewController 中,我以编程方式创建了一个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。

在 Interface Builder 中,我使用“模拟指标”,它会自动将视图的高度设置为 367.00。

我是否缺少某些东西,无论我尝试什么,我都看不到我需要的 367.00 高度。

作为临时修复,我手动设置了 UITableView 的框架,这并不理想,所以最好弄清楚为什么这不起作用:

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

【问题讨论】:

  • 也许你应该检查一下tableView的autoSizingMasks
  • 我已经用我将autoresizingMask 值设置为的内容更新了我的原始帖子。

标签: objective-c ios uitableview uinavigationcontroller uitabbarcontroller


【解决方案1】:

您应该将UINavigationController 实例添加到UITabBarController,然后将表格视图控制器添加到UINavigationController 实例的rootViewController 属性,这应该会让您的生活更轻松。

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

将您的 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 实例以编程方式创建并添加到窗口中。

通过继承 UITableViewController 类,您可以避免以编程方式创建 UITableView 实例并获得开箱即用的大部分所需内容。

当您需要将详细视图推送到 UINavigationController 实例的堆栈中时,您只需使用类似于此的内容:

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

这将为您将详细视图 UIViewController 子类添加到 UINavigationController 实例的视图层次结构中并为过渡设置动画。

其中有很多控制器,但这是完全值得的,并且会避免您遇到的很多问题,因为这种方法允许视图管理调整大小并自行考虑工具栏/导航栏。

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 我尝试输出 self.view.bounds.size.height 的值,它再次返回 460.00。
    • 你在哪里尝试这样做。它将在 -(void)viewDidAppear:(BOOL)animated 中更新; (也许在 -(void)viewWillAppear:(BOOL)animated; 还有,但我不确定)
    • 您应该注意的另一件事是 WantsFullScreenLayout 属性。如果你为你的 UIViewController 将它设置为 YES,它将相应地更新你的视图,而不考虑标签栏。
    • 啊,我明白了,如果我在 viewDidAppear 方法中输出边界,它会返回正确的结果。目前我正在viewDidLoad中设置我的UITableView,我会在viewDidAppear中尝试一下。
    • 最好在 viewDidAppear 中进行设置,并且只在 viewDidAppear 中应用帧更改,但在这种情况下,您需要持有指向 tableView 的指针。
    猜你喜欢
    • 2017-01-24
    • 2015-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-02
    相关资源
    最近更新 更多