如何在导航和选项卡控制器中正确添加 UITableView?

How to properly add a UITableView inside a nav and tab controller?

我有一个导航设置,顶部有一个 UITabBarController。然后我有一个选项卡,它是通过创建一个 UIViewController 实例化到 UINavigationController 中的,如下所示:

UIViewController *testVC = [UIViewController new]; // Has UITableView as subview
UINavigationController *testNavVC = [[UINavigationController alloc] initWithRootViewController:testVC];

[self setViewControllers:@[testNavVC]];

出现的问题是 testVC UIViewController 中的 UITableView。 table 正确显示在顶部并正确位于 UINavController 的导航栏下方。但是,当您将 table 视图滚动到底部时,table 视图中的最后几行将在屏幕底部被截断。我发现我可以将底部内容插入设置为 100(值会因行高而异)以正确显示内容。不过我觉得我不需要这样做,我正在寻找更好的解决方案。

如何正确添加以这种方式嵌套的 UITableView?

作为旁注,当使用 UITableViewController 而不是带有添加的 UITableView 的 UIViewController 时,这一切都可以正常工作。就我而言,我需要使用后一个选项。

您可以尝试使用 bottomLayoutGuide 属性:

tableView.contentInset = UIEdgeInsetsMake(0.0, 0.0, self.bottomLayoutGuide.length, 0.0);

它表示内容的最低垂直范围,可以从iOS 7开始使用。

作为替代方案,您可以使用此值为 UITableView 创建底部 NSLayoutConstraint。

我的所有代码都是以编程方式完成的,最终问题是我使用视图框架设置了 UITableView。我将其切换为使用自动布局,效果很好!