【问题标题】:Custom Segue to UINavigationController shows black screen自定义 Segue 到 UINavigationController 显示黑屏
【发布时间】:2017-07-26 23:48:09
【问题描述】:

我正在使用从 UIViewController 到 UINavigationController 的简单自定义 segue,该 UINavigationController 将 UICollectionViewController 作为它的根视图控制器。

转换按预期工作,但我首先看到一个黑屏,只有当转换结束时我才能看到 UICollectionViewController 内容。

故事板:

转场:

这是自定义转场的代码:

- (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 时,集合视图会正常加载,没有黑屏。

知道我在这里做错了什么吗?

【问题讨论】:

    标签: ios objective-c iphone uiviewcontroller uinavigationcontroller


    【解决方案1】:

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

    UINavigationController *destinationViewController = self.destinationViewController;
    

    然后你想用

    destinationViewController.topViewController.view.transform = ...
    

    因此,您在屏幕上尚未显示的视图上进行操作。

    然后在动画之后你会这样做:

    [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)
            }
        }
    

    当然,当你使用自动布局是或否时,代码会有所不同......但你应该从中得到一个基本的想法。

    【讨论】:

    • 谢谢。我理解我的代码与您的示例有什么问题
    【解决方案2】:

    当你提交你的动画时,你改变了当前控制器的视图,结果离开了窗口,或者 rootViewController 视图的背景颜色是黑色的。在动画持续时间内看到“黑屏”是有道理的 --- 0.25 秒

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-18
      • 1970-01-01
      • 1970-01-01
      • 2013-04-21
      • 1970-01-01
      相关资源
      最近更新 更多