UPDATE

Based on Tim's answer, I implemented the following in each view controller that had a scrollview (or subclass) that was part of my custom container:

- (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];
}

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

I have custom container view controller called SegmentedPageViewController. I set this as a UINavigationController's rootViewController.

The purpose of SegmentedPageViewController is to allow a UISegmentedControl, set as the NavController's titleView, to switch between different child view controllers.

enter image description here

These child view controllers all contain either a scrollview, tableview, or collection view.

We're finding that the first view controller loads fine, correctly positioned underneath the navigation bar. But when we switch to a new view controller, the navbar isn't respected and the view is set underneath the nav bar.

enter image description here

We're using auto layout and interface builder. We've tried everything we can think of, but can't find a consistent solution.

Here's the main code block responsible for setting the first view controller and switching to another one when a user taps on the segmented control:

- (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;
        }
    }

}

推荐答案

Your custom container view controller will need to adjust the contentInset of the second view controller according to your known navigation bar height, respecting the automaticallyAdjustsScrollViewInsets property of the child view controller. (You may also be interested in the topLayoutGuide property of your container - make sure it returns the right value during and after the view switch.)

UIKit is remarkably inconsistent (and buggy) in how it applies this logic; sometimes you'll see it perform this adjustment automatically for you by reaching multiple view controllers down in the hierarchy, but often after a custom container switch you'll need to do the work yourself.

Objective-c相关问答推荐

如何删除Objective-C中未定义的符号&错误?

日期更改时的 UIDatePicker

OCUnit 的单元测试示例

iPhone 可达性判断

如何声明仅调试语句

避免NSArray 在被枚举时发生Mutations

如何在 XCode 中复制文件?

以编程方式创建 UITableView

使用 Objective-C 查找 IMEI 号码

在我的 iOS Mail 和 Safari 应用程序中支持 Open In... 菜单项

StoryBoard 助理编辑器停止显示相关文件

在 NSString 对象中查找子字符串

如果目录不存在,是否有更安全的方法来创建目录?

Cocoa 的依赖注入框架?

如何在 iOS 7 启动期间更改状态栏样式

使用 NSLog 打印 NSData

判断是否已实现可选协议方法

UITableViewCell 点击展开

选中时选中的 UItableViewCell 保持蓝色

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