嵌入自定义容器视图控制器时,内容位于导航栏下方。 [英] Content falls beneath navigation bar when embedded in custom container view controller.

查看:87
本文介绍了嵌入自定义容器视图控制器时,内容位于导航栏下方。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新

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

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

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

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

我有自定义容器视图控制器,名为 SegmentedPageViewController 。我把它设置为 UINavigationController的rootViewController

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

SegmentedPageViewController的目的允许设置为NavController的titleView的 UISegmentedControl ,以在不同的子视图控制器之间切换。

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

这些子视图控制器所有都包含scrollview,tableview或集合视图。

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.

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

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

}


推荐答案

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

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在如何应用这种逻辑方面非常不一致(并且有错误);有时您会看到它通过在层次结构中向下到达多个视图控制器来自动执行此调整,但通常在自定义容器切换后您需要自己完成工作。

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.

这篇关于嵌入自定义容器视图控制器时,内容位于导航栏下方。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆