Taras Tomchuk
Taras Tomchuk

Reputation: 331

Hide switching of storyboards

In my application I have 2 storyboards: Onboarding and Main. When user opens app for a first time - Onboarding storyboard is presented. After that, he presses a button and I show him Main Storyboard with such code:

let storyboard = UIStoryboard(name: "Shop", bundle: nil)
let navigationVc = storyboard.instantiateViewController(withIdentifier: "ShopScreens") as UIViewController
navigationVc.modalPresentationStyle = UIModalPresentationStyle.overCurrentContext
self.present(navigationVc, animated: false, completion: nil)

I wanted to make custom animation, when user switches storyboards. To do so - I've created simple view that I'm presenting on top of everything like this:

UIApplication.shared.keyWindow?.addSubview(self.containerViewForAnimatedView)

And this view is working fine, but only in terms of one Storyboard, it covers everything while app changes screens.

But when I try to switch storyboards - this view gets covered by newly presented storyboard.

I also tried presenting view in such way and brining it to front:

let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.window?.addSubview(self.containerViewForAnimatedView)

But that din't work as well.

How can I hide switching of storyboards by presenting custom created view during that transition? Would be grateful for any help.

Upvotes: 0

Views: 48

Answers (1)

Vollan
Vollan

Reputation: 1915

Just played around some with it, no animations or anything special but this will give you the flow that you want:

class ViewController: UIViewController {
    let viiew = UIView.init(frame: UIScreen.main.bounds)

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .red

        DispatchQueue.main.asyncAfter(deadline: .now() + 3) {
            self.viiew.backgroundColor = .blue
            UIApplication.shared.keyWindowInConnectedScenes?.resignKey()
            UIApplication.shared.keyWindowInConnectedScenes?.addSubview(self.viiew)
            self.viiew.layer.zPosition = 1000
        }

        DispatchQueue.main.asyncAfter(deadline: .now() + 4) {
            let vc = ViewController2()
            UIApplication.shared.keyWindowInConnectedScenes?.rootViewController = vc
            DispatchQueue.main.asyncAfter(deadline: .now() + 4) {
                self.viiew.removeFromSuperview()
            }
        }
    }


}

class ViewController2: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .green


    }
}

extension UIApplication {

    var keyWindowInConnectedScenes: UIWindow? {
        return windows.first(where: { $0.isKeyWindow })
    }

}

Upvotes: 1

Related Questions