在子视图控制器中调整滚动视图的插入

Adjust scroll view's inset within a child view controller

我得到了一个具有以下父子关系的视图控制器层次结构:

UINavigationController (contains) MainViewController (contains) UIPageViewController (contains) UITableViewController

在最里面的 UITableViewController 中,我将 automaticallyAdjustScrollViewInset 设置为 YES,但是这个设置似乎不起作用。从下面的屏幕截图可以看出,table 视图的 contentInset 似乎没有随导航栏进行调整。

我的目标是让这个 table 视图的 contentInset 自动调整最外层的导航栏。如果导航栏或状态栏被隐藏,我希望内容插入相应地调整。

我该怎么做?谢谢!

显然,最外层的 UINavigationController 仍然可以通过其 navigationController 属性.

被最内层的 UITableViewController 访问

因为最里面的视图控制器可以访问导航栏的框架,所以我们可以在它的 viewDidLayoutSubviews 中做这样的事情。

- (void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];

    self.tableView.contentInset = UIEdgeInsetsMake(CGRectGetMaxY(self.navigationController.navigationBar.frame), 0, 0, 0);
    self.tableView.scrollIndicatorInsets = self.tableView.contentInset;
}

UINavigationController 自动调整其子视图控制器的 topLayoutGuide

在你的情况下,它传播得不够远。

如果您确保此 topLayoutGuide 将其归结到您的 table 视图控制器,那么您将不必手动设置 contentInset

此外,您的视图控制器层次结构似乎过于复杂(我不知道您项目的详细信息)。你所拥有的这个简化版本将免费为你提供你想要的东西。