[英]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.