简体   繁体   中英

Present view from app delegate with a tabbar Controller-> Nav controller -> View Hierarchy - iOS Swift

I have searched throughout SO and have found similar articles, but none of the solutions worked for me. Below is a screenshot of my view hierarchy 在此处输入图片说明

One of the many snippets of code I have tried is as follows:

            var storyBoard = UIStoryboard()
            if UIDevice.current.userInterfaceIdiom == .pad {
                storyBoard = UIStoryboard(name: "iPad", bundle: nil)
            } else {
                storyBoard = UIStoryboard(name: "Main", bundle: nil)
            }


            let tabVc = self.window?.rootViewController as! UITabBarController


            let mynVC = storyBoard.instantiateViewController(withIdentifier: "nav") as! UINavigationController


            let calcVc = storyBoard.instantiateViewController(withIdentifier: "destVc")


            tabVc.present(mynVC, animated: true, completion: { () -> Void in
              // Segue , do stuff

            })

The above code presents the correct tab with a nav bar, but there is no tabor present.

I would like to have a force touch button that presents the tab with the navigation bar AND the tabbar present, does anyone know how to do that?

I'm not able to fully test this at the moment, but I believe that in a tab bar, you dont present the view controller. you set its view controller items and selectedViewController properties.

This should work, I'll test it and update shortly,

var storyBoard = UIStoryboard()
        if UIDevice.current.userInterfaceIdiom == .pad {
            storyBoard = UIStoryboard(name: "iPad", bundle: nil)
        } else {
            storyBoard = UIStoryboard(name: "Main", bundle: nil)
        }


        let tabVc = self.window?.rootViewController as! UITabBarController

        let mynVC = storyBoard.instantiateViewController(withIdentifier: "nav") as! UINavigationController

        let calcVc = storyBoard.instantiateViewController(withIdentifier: "destVc")

        tabVc.viewControllers = [mynVC]
        tabVc.selectedViewController = mynVC

Depending on what else you have below, you may also need to call window.setKeyAndVisible() to actually show

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM