[英]Custom Segue to UINavigationController shows black screen
我正在使用从UIViewController到UINavigationController的简单自定义设置,该UINavigationController将UICollectionViewController保留为根视图控制器。
过渡按预期方式工作,但是我首先看到黑屏,并且只有在过渡结束后才能看到UICollectionViewController内容。
故事板:
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
}];
}
注意:
当我运行简单的show
segue时,将按原样加载集合视图,没有黑屏。
任何想法我在这里做错了吗?
好的,让我们浏览一下目标视图控制器,因为它看起来是黑色的:您正在引用目标视图控制器:
UINavigationController *destinationViewController = self.destinationViewController;
然后您要用它制作动画
destinationViewController.topViewController.view.transform = ...
因此,您可以在屏幕上尚未显示的视图上进行操作。
然后在动画之后,您需要这样做:
[destinationViewController.topViewController.view removeFromSuperview]; // remove from temp super view
因此,您实际上是在删除UIViewController超级视图,而不是“ temp”超级视图 !
所以当你下一步
[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)
}
}
当然,当您使用自动布局是或否时,代码将有所不同。但是您应该从中得到一个基本的想法。
提交动画时,将变换当前控制器的视图,结果是离开窗口,或视图背景色为黑色的rootViewController。 您可以看到动画持续时间为0.25秒的“黑屏”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.