简体   繁体   English

UINavigationController的自定义Segue显示黑屏

[英]Custom Segue to UINavigationController shows black screen

I'm using a simple custom segue from my UIViewController to a UINavigationController which holds UICollectionViewController as it's root view controller. 我正在使用从UIViewController到UINavigationController的简单自定义设置,该UINavigationController将UICollectionViewController保留为根视图控制器。

The transition works as expected, but i first see a black screen and only when the transition has ended i see the UICollectionViewController content. 过渡按预期方式工作,但是我首先看到黑屏,并且只有在过渡结束后才能看到UICollectionViewController内容。

Storyboard: 故事板:

在此处输入图片说明

Segue: Segue公司:

在此处输入图片说明

This is the code for the custom segue: 这是定制segue的代码:

- (void)perform {
    UIViewController *sourceViewController = self.sourceViewController;
    UINavigationController *destinationViewController = self.destinationViewController;


    [UIView animateWithDuration:0.25
                          delay:0.0
                        options:UIViewAnimationOptionCurveEaseInOut
                     animations:^{
                         destinationViewController.topViewController.view.transform =
                         CGAffineTransformMakeTranslation(0, 0);
                         sourceViewController.view.transform =
                         CGAffineTransformMakeTranslation(-sourceViewController.view.frame.size.width, 0);
                     }
                     completion:^(BOOL finished){
                         [destinationViewController.topViewController.view removeFromSuperview]; // remove from temp super view
                         [sourceViewController presentViewController:destinationViewController animated:NO completion:NULL]; // present VC
                     }];
}

NOTE: 注意:

When i am running a simple show segue, the collection view is loaded as it should, No black screen. 当我运行简单的show segue时,将按原样加载集合视图,没有黑屏。

Any idea what i'm doing wrong here? 任何想法我在这里做错了吗?

Ok let's go through your destination view controller because it appear to be black: You're doing a reference to your destination view controller: 好的,让我们浏览一下目标视图控制器,因为它看起来是黑色的:您正在引用目标视图控制器:

UINavigationController *destinationViewController = self.destinationViewController;

Then you want to animate it with 然后您要用它制作动画

destinationViewController.topViewController.view.transform = ...

So you operate on view that is not shown on the screen yet. 因此,您可以在屏幕上尚未显示的视图上进行操作。

Then after animation you're doint this: 然后在动画之后,您需要这样做:

[destinationViewController.topViewController.view removeFromSuperview]; // remove from temp super view

So you're actually here removing UIViewController superview not "temp" superview ! 因此,您实际上是在删除UIViewController超级视图,而不是“ temp”超级视图

So when you in the next step 所以当你下一步

[sourceViewController presentViewController:destinationViewController animated:NO completion:NULL]; // present VC

Doing that your destinationViewController has no view whatsoever, so it appear as black screen. 这样,您的destinationViewController就没有任何视图,因此它显示为黑屏。

To fix it copy a view animate and remove when destination vc is loaded. 要修复此问题,请在加载目标vc时复制视图动画并删除。 I believe that you had exactly this in mind when I look at your code. 我认为在查看您的代码时,您正是牢记这一点。

So the base idea is: 因此,基本思路是:

override func perform() {
        let sourceVC = self.source
        let destinationVC = self.destination
        let view : UIView = UIView()
        view.frame = CGRect(x: sourceVC.view.frame.size.width, y: 0, width: sourceVC.view.frame.size.width, height: sourceVC.view.frame.size.height)
        view.addSubview(destinationVC.view)
        sourceVC.view.addSubview(view)

        UIView.animate(withDuration: 2, animations: {
            sourceVC.view.transform =
                CGAffineTransform(translationX: -sourceVC.view.frame.size.width, y: 0);

        }) { completion in
            view.removeFromSuperview()
            sourceVC.present(destinationVC, animated: false, completion: nil)
        }
    }

Of course code will be different when u use autolayout yes or no... But you should get from it a basic idea. 当然,当您使用自动布局是或否时,代码将有所不同。但是您应该从中得到一个基本的想法。

在此处输入图片说明

when you commit your animation, you transform the view of current controller, resultly there left the window, or rootViewController which's view's backgroundcolor is black. 提交动画时,将变换当前控制器的视图,结果是离开窗口,或视图背景色为黑色的rootViewController。 it makes sense you see the 'black screen' for animation duration --- 0.25s 您可以看到动画持续时间为0.25秒的“黑屏”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM