LoginSignup
0
5

More than 3 years have passed since last update.

iOS13 rootViewController 切替メモ

Last updated at Posted at 2020-07-03

rootViewController 切替

iOS 13 から rootViewController の持ち方が SceneDelegate に代わりました。rootViewController切り替えの例です。

UIWindow, rootViewController の扱い

UIWindow, rootViewControllerは SceneDelegate で管理します。
ビューコントローラーを切り替えたい場合は、次のようにします。

let sceneDelegate = UIApplication.shared.connectedScenes.first!.delegate as! SceneDelegate
sceneDelegate.window!.rootViewController = 新しいビューコントローラー

アニメーション効果

上記だけでと一瞬で画面が切り替わってしまうので、若干のアニメーション効果をつけてみます。
UIWindow間でのaddSubviewなどもできなくなってるっぽいので、やり方の例としてこんな風な処理をおこなっています。
1.遷移元の画面を画像化し、遷移先の画面に配置
2.rootViewController を入れ替える
3.アニメーションを利用して 1.の画像を透明化して削除

まず先にUIViewを画像化する処理を作成します。

extension UIView {
    // UIVIewを画像化
    func GetImage() -> UIImage {
        // キャプチャする範囲を取得.
        let rect = self.bounds
        // ビットマップ画像のcontextを作成.
        UIGraphicsBeginImageContextWithOptions(rect.size, false, 0.0)
        let context: CGContext = UIGraphicsGetCurrentContext()!
        // 対象のview内の描画をcontextに複写する.
        self.layer.render(in: context)
        // 現在のcontextのビットマップをUIImageとして取得.
        let capturedImage : UIImage = UIGraphicsGetImageFromCurrentImageContext()!
        // contextを閉じる.
        UIGraphicsEndImageContext()
        return capturedImage
    }
}

rootViewController を切り替える処理を作成します。

extension UIViewController {
    // rootViewController を入れ替える画面遷移
    func moveTo(_ viewController: UIViewController) {
        // 元画面の画像をセット
        let imageView = UIImageView(image: self.view.GetImage())
        viewController.view.addSubview(imageView)

        // rootViewController入れ替え
        let sceneDelegate = UIApplication.shared.connectedScenes.first!.delegate as! SceneDelegate
        sceneDelegate.window!.rootViewController = viewController

        // 元画面の画像を削除
        UIView.animate(withDuration: 0.5, animations: {
            imageView.alpha = 0.0
        }, completion: { _ in
            imageView.removeFromSuperview()
        })
    }
}

self.moveTo(遷移先のビューコントローラー) で処理を実装できます。

Simulator Screen Shot - iPhone 11 Pro - 2020-07-04 at 02.33.43.png Simulator Screen Shot - iPhone 11 Pro - 2020-07-04 at 02.33.46.png Simulator Screen Shot - iPhone 11 Pro - 2020-07-04 at 02.34.01.png Simulator Screen Shot - iPhone 11 Pro - 2020-07-04 at 02.34.02.png
0
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
5