56

更新

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

- (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是允许一个UISegmentedControl,设置为 NavController 的 titleView,在不同的子视图控制器之间切换。

在此处输入图像描述

这些子视图控制器都包含滚动视图、表格视图或集合视图。

我们发现第一个视图控制器加载良好,正确定位在导航栏下方。但是当我们切换到一个新的视图控制器时,导航栏不被尊重并且视图被设置在导航栏下方。

在此处输入图像描述

我们正在使用自动布局和界面构建器。我们已经尝试了我们能想到的一切,但找不到一致的解决方案。

这是负责设置第一个视图控制器并在用户点击分段控件时切换到另一个的主要代码块:

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

}
4

5 回答 5

27

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

UIKit 在如何应用这个逻辑方面非常不一致(并且有问题);有时您会看到它通过到达层次结构中的多个视图控制器自动为您执行此调整,但通常在自定义容器切换之后您需要自己完成工作。

于 2013-09-26T23:05:26.423 回答
24

这似乎比人们想象的要简单得多。

UINavigationController 只会在布局子视图时设置滚动视图插图。addChildViewController:但是,不会导致布局,因此在调用它之后,您只需要调用setNeedsLayout您的导航控制器。这是我在自定义选项卡式视图中切换视图时所做的事情:

[self addChildViewController:newcontroller];
[self.view insertSubview:newview atIndex:0];
[self.navigationController.view setNeedsLayout];

最后一行将导致为新视图控制器的内容重新计算滚动视图插图。

于 2015-10-26T11:21:46.007 回答
9

仅供参考,以防有人遇到类似问题:即使没有嵌入式视图控制器,也会出现此问题。似乎automaticallyAdjustsScrollViewInsets只有当您的滚动视图(或 tableview/collectionview/webview)是其视图控制器层次结构中的第一个视图时才适用。

我经常在我的层次结构中首先添加一个 UIImageView 以获得背景图像。如果这样做,则必须在 viewDidLayoutSubviews 中手动设置滚动视图的边缘插图:

- (void) viewDidLayoutSubviews {
    CGFloat top = self.topLayoutGuide.length;
    CGFloat bottom = self.bottomLayoutGuide.length;
    UIEdgeInsets newInsets = UIEdgeInsetsMake(top, 0, bottom, 0);
    self.collectionView.contentInset = newInsets;

}
于 2014-08-17T18:28:02.377 回答
1

我找到了一个更好的解决方案,使用 UINavigationController 的未记录方法。

#import <UIKit/UIKit.h>

@interface UINavigationController (ContentInset)


- (void) computeAndApplyScrollContentInsetDeltaForViewController:(UIViewController*) controller;

@end

#import "UINavigationController+ContentInset.h"

@interface UINavigationController()


- (void)_computeAndApplyScrollContentInsetDeltaForViewController:(id)arg1; 

@end


@implementation UINavigationController (ContentInset)

- (void) computeAndApplyScrollContentInsetDeltaForViewController:(UIViewController*) controller
{
    if ([UINavigationController instancesRespondToSelector:@selector(_computeAndApplyScrollContentInsetDeltaForViewController:)])
        [self _computeAndApplyScrollContentInsetDeltaForViewController:controller];
}

@end

然后,这样做

- (void) cycleFromViewController: (UIViewController*) oldC
                toViewController: (UIViewController*) newC
{
    [oldC willMoveToParentViewController:nil];                        
    [self addChildViewController:newC];
  
    [self transitionFromViewController: oldC toViewController: newC   
                              duration: 0.25 options:0
                            animations:^{
                                newC.view.frame = oldC.view.frame;                                    
                                [self.navigationController computeAndApplyScrollContentInsetDeltaForViewController:newC];
                            }
                            completion:^(BOOL finished) {
                                [oldC removeFromParentViewController];                  
                                [newC didMoveToParentViewController:self];
                            }];
}

于 2015-08-30T09:15:41.333 回答
0

设置edgesForExtendedLayout = []我的 childcontrollers 对我有用。

于 2018-05-22T13:47:28.210 回答