UPDATE

根据Tim的回答,我在每个具有scrollview(或子类)的视图控制器中实现了以下内容,它是我的自定义容器的一部分:

- (void)didMoveToParentViewController:(UIViewController *)parent
{
    if (parent) {
        CGFloat top = parent.topLayoutGuide.length;
        CGFloat bottom = parent.bottomLayoutGuide.length;

        // this is the most important part here, because the first view controller added 
        // never had the layout issue, it was always the second. if we applied these
        // edge insets to the first view controller, then it would lay out incorrectly.
        // first detect if it's laid out correctly with the following condition, and if
        // not, manually make the adjustments since it seems like UIKit is failing to do so
        if (self.collectionView.contentInset.top != top) {
            UIEdgeInsets newInsets = UIEdgeInsetsMake(top, 0, bottom, 0);
            self.collectionView.contentInset = newInsets;
            self.collectionView.scrollIndicatorInsets = newInsets;
        }
    }

    [super didMoveToParentViewController:parent];
}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

我有一个名为SegmentedPageViewController的定制容器视图控制器.我把它设为UINavigationController's rootViewController.

SegmentedPageViewController的用途是允许设置为NavController标题视图的UISegmentedControl在不同的子视图控制器之间切换.

在此处输入图像描述

这些子视图控制器都包含scrollview、tableview或collection视图.

我们发现,第一个视图控制器加载良好,正确定位在导航栏下方.但是当我们换到一个新的

在此处输入图像描述

我们正在使用自动布局和界面生成器.我们已经想尽一切办法,但找不到一致的解决方案.

以下是主要代码块,用于设置第一个视图控制器,并在用户点击分段控件时切换到另一个视图控制器:

- (void)switchFromViewController:(UIViewController *)oldVC toViewController:(UIViewController *)newVC
{
    if (newVC == oldVC) return;

    // Check the newVC is non-nil otherwise expect a crash: NSInvalidArgumentException
    if (newVC) {

        // Set the new view controller frame (in this case to be the size of the available screen bounds)
        // Calulate any other frame animations here (e.g. for the oldVC)
        newVC.view.frame = self.view.bounds;

        // Check the oldVC is non-nil otherwise expect a crash: NSInvalidArgumentException
        if (oldVC) {
            // **** THIS RUNS WHEN A NEW VC IS SET ****
            // DIFFERENT FROM FIRST VC IN THAT WE TRANSITION INSTEAD OF JUST SETTING


            // Start both the view controller transitions
            [oldVC willMoveToParentViewController:nil];
            [self addChildViewController:newVC];

            // Swap the view controllers
            // No frame animations in this code but these would go in the animations block
            [self transitionFromViewController:oldVC
                              toViewController:newVC
                                      duration:0.25
                                       options:UIViewAnimationOptionLayoutSubviews
                                    animations:^{}
                                    completion:^(BOOL finished) {
                                        // Finish both the view controller transitions
                                        [oldVC removeFromParentViewController];
                                        [newVC didMoveToParentViewController:self];
                                        // Store a reference to the current controller
                                        self.currentViewController = newVC;
                                    }];
        } else {

            // **** THIS RUNS WHEN THE FIRST VC IS SET ****
            // JUST STANDARD VIEW CONTROLLER CONTAINMENT

            // Otherwise we are adding a view controller for the first time
            // Start the view controller transition
            [self addChildViewController:newVC];

            // Add the new view controller view to the view hierarchy
            [self.view addSubview:newVC.view];

            // End the view controller transition
            [newVC didMoveToParentViewController:self];

            // Store a reference to the current controller
            self.currentViewController = newVC;
        }
    }

}

推荐答案

根据子视图控制器的automaticallyAdjustsScrollViewInsets属性,自定义容器视图控制器需要根据已知的导航栏高度调整第二个视图控制器的contentInset.(您可能还对容器的topLayoutGuide属性感兴趣——确保它在视图切换期间和之后返回正确的值.)

UIKit在如何应用这种逻辑方面非常不一致(而且有缺陷);有时,您会看到它通过到达层次 struct 中的多个视图控制器来自动执行此调整,但通常在自定义容器切换后,您需要自己完成这项工作.

Objective-c相关问答推荐

将 NSNumber 转换为 NSDecimalNumber

为什么我的 UIButton 的内部修饰事件没有被调用?

如何在 iOS 中拨打电话?

如何判断字符串是否仅包含目标C中的字母数字字符?

设置数据后调整 UICollectionView 单元格的大小

如何将 .plist 文件中的数据 struct 读入 NSArray

未知类型名称类;您指的是 'Class' 吗?

无法在 Interface Builder (Xcode 5) 中创建与子视图的出口连接

alloc 和 allocWithZone: 有什么区别?

UICollectionView 单元格 Select 和单元格重用

如何将 NSNumber 转换和比较为 BOOL?

Objective-c 协议前向声明

Objective-c 2.0 中的可选参数?

writeToFile:atomically: atomically 是什么意思?

为什么我们不能在当前队列上使用 dispatch_sync?

BOOL到 NSString

UIScrollView contentSize 不起作用

如何更新 Core Data 中的现有对象?

如何设置字体大小以填充 UILabel 高度?

如何将按钮放在不会在 iOS 中随表格滚动的 UITableView 上