自定义Segue到UINavigationController显示黑屏

问题描述:

我使用从我的UIViewController到UINavigationController的简单自定义segue,UINavigationController保存了UICollectionViewController,因为它是根视图控制器。自定义Segue到UINavigationController显示黑屏

过渡按预期工作,但我首先看到一个黑屏,并且只有当过渡结束时才看到UICollectionViewController内容。

故事板:

enter image description here

Segue公司:

enter image description here

这是自定义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 
        }]; 
} 

注:

当我运行一个简单的show segue时,集合视图会按照它应该加载的方式加载,没有黑屏。

任何想法我在这里做错了吗?

好吧,让我们通过你的目标视图控制器,因为它看起来是黑色的: 你正在做你的目标视图控制器的引用:

UINavigationController *destinationViewController = self.destinationViewController; 

那么你一定要与

destinationViewController.topViewController.view.transform = ... 

所以,你在未显示在屏幕上尚未视图操作动画吧。

然后动画完成后,你doint此:

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

所以你实际上是在这里删除的UIViewController上海华不是 “临时” 上海华

所以,当你在下一步

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

这样做,你的destinationViewController没有任何看法,所以它显示为黑屏。

修复它复制视图动画和删除目标vc加载时。我相信,当我看着你的代码时,你完全明白这一点。

所以基本思路是:

当然
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) 
     } 
    } 

代码将是不同的,当u使用自动布局yes或no ...但是你应该从它那里得到一个基本思路。

enter image description here

+1

谢谢。我用你的例子了解我的代码有什么问题 – Itzik984

当您提交动画时,会转换当前控制器的视图,结果会留下窗口,或者视图的backgroundcolor为rootViewController为黑色。它是有道理的,你看动画持续时间的'黑屏'--- 0.25s