2016-12-14 7 views
12

タブバーを使って別のルートビューコントローラにスワップしようとしています。アプリケーションデリゲートを介して、私はトランジションアニメーションを追加したい。デフォルトではアニメーションなしのビューのみが表示されます。rootViewControllerをアニメーションでスワップしますか?

let tabBar = self.instantiateViewController(storyBoard: "Main", viewControllerID: "MainTabbar") 
let appDelegate = UIApplication.shared.delegate as! AppDelegate 
appDelegate.window = UIWindow(frame: UIScreen.main.bounds) 
appDelegate.window?.rootViewController = tabBar 
appDelegate.window?.makeKeyAndVisible() 

これは、別のrootviewコントローラに置き換えた方法です。

答えて

52

私はいくつかの時間前UIView.transition(from: view, to: view)UIView.transition(with: view)を評価し、これを使用して終了:交換しようとしている(残念ながら、なぜ覚えていないことができます)

guard let window = UIApplication.shared.keyWindow else { 
    return 
} 

guard let rootViewController = window.rootViewController else { 
    return 
} 

let storyboard = UIStoryboard(name: "Main", bundle: nil) 
let vc = storyboard.instantiateViewController(withIdentifier: "MainTabbar") 
vc.view.frame = rootViewController.view.frame 
vc.view.layoutIfNeeded() 

UIView.transition(with: window, duration: 0.3, options: .transitionCrossDissolve, animations: { 
    window.rootViewController = vc 
}, completion: { completed in 
    // maybe do something here 
}) 
+0

はうまくやった別の代替ソリューション(Y)。 – Itzdsp

+0

ありがとうthats私を助ける –

1

これを試してみてください:

UIView.transition(from: appdelegate.window.rootViewController!.view, to: tabbar.view, duration: 0.6, options: [.transitionCrossDissolve], completion: { 
    _ in 
    appdelegate.window.rootViewController = tabbar 
}) 
2

イム別のルートビューコントローラに...そして、私はトランジションアニメーションを追加したいです

私はこれを行うアプリケーションを持っています:それはアニメーション(これは、アルバムと呼ばれる)とルートビューコントローラを変更します。

実際に私のアプリはではありません実際にルートビューコントローラを変更します。ルートビューコントローラは、決して変更されないカスタムコンテナビューコントローラです。そのビューは決して見られず、機能もありません。その唯一の仕事は、変化が起こる場所であることです。それは、1つの子ビューコントローラを別の子ビューコントローラに入れ替えることで、トランジションアニメーションが機能します。

つまり、View Controller階層に、階層の最上部にある1つのView Controllerを追加すると、問題全体がきれいに正しく解決されます。

1

let rootVC = ViewControllers.getInstance.getMainTabBarController() 
    let snapshot = (UIApplication.shared.keyWindow?.snapshotView(afterScreenUpdates: true))! 
    rootVC.view.addSubview(snapshot); 

    UIApplication.shared.keyWindow?.rootViewController = rootVC; 
    UIView.transition(with: snapshot, duration: 0.4, options: .transitionCrossDissolve, animations: { 
     snapshot.layer.opacity = 0; 
    }, completion: { (status) in 
     snapshot.removeFromSuperview() 
    }) 
関連する問題