如何将rootviewcontroller更改为堆栈中的第三个viewcontroller

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何将rootviewcontroller更改为堆栈中的第三个viewcontroller相关的知识,希望对你有一定的参考价值。

var window: UIWindow?



    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

        let tabBarController = UITabBarController()

        let settingStoryboard = UIStoryboard(name: "Setting", bundle: nil)
        let toDoListStoryboard = UIStoryboard(name: "ToDoList", bundle: nil)
        let homeStoryboard = UIStoryboard(name: "Home", bundle: nil)
        let eventListStoryboard = UIStoryboard(name: "EventList", bundle: nil)
        let activityStoryboard = UIStoryboard(name: "Activity", bundle: nil)

        let settingVC = settingStoryboard.instantiateViewController(withIdentifier: "Setting") as! SettingViewController
        let toDoListVC = toDoListStoryboard.instantiateViewController(withIdentifier: "ToDoList") as! ToDoListViewController
        let homeVC = homeStoryboard.instantiateViewController(withIdentifier: "Home") as! HomeViewController
        let eventListVC = eventListStoryboard.instantiateViewController(withIdentifier: "EventList") as! EventListViewController
        let activityVC = activityStoryboard.instantiateViewController(withIdentifier: "Activity") as! ActivityViewController

        let vcData: [(UIViewController, UIImage, UIImage)] = [

            (settingVC, UIImage(named: "SettingIcon")!, UIImage(named: "SettingSelectedIcon")!),
            (toDoListVC, UIImage(named: "ToDoIcon")!, UIImage(named: "ToDoSelectedIcon")!),
            (homeVC, UIImage(named: "HomeIcon")!, UIImage(named: "HomeSelectedIcon")!),
            (eventListVC, UIImage(named: "EventIcon")!, UIImage(named: "EventSelectedIcon")!),
            (activityVC, UIImage(named: "ActivityIcon")!, UIImage(named: "ActivitySelectedIcon")!)
        ]

        let vcs = vcData.map { (vc, image, selectedImage) -> UINavigationController in

            let nav = UINavigationController(rootViewController: vc)

            nav.tabBarItem.image = image.withRenderingMode(UIImage.RenderingMode.alwaysOriginal)
            nav.tabBarItem.selectedImage = selectedImage.withRenderingMode(UIImage.RenderingMode.alwaysOriginal)

            nav.tabBarItem.imageInsets = UIEdgeInsets(top: 7, left: 0, bottom: -5, right: 0)

            return nav
        }

        tabBarController.viewControllers = vcs
        tabBarController.tabBar.isTranslucent = false
        tabBarController.tabBar.barTintColor = UIColor(red: 19/255, green: 41/255, blue: 77/255, alpha: 1)

        UINavigationBar.appearance().barTintColor = UIColor(red: 19/255, green: 41/255, blue: 77/255, alpha: 1)
        UINavigationBar.appearance().isTranslucent = false

        window?.rootViewController = tabBarController

        print(Realm.Configuration.defaultConfiguration.fileURL!)

        return true
    }

在上面显示的代码中,当app运行时,rootviewcontroller是添加到堆栈的第一个视图。如何在不首先初始化homeviewcontroller的情况下将rootviewcontroller更改为HomeViewController。

答案

只需添加

tabBarController.selectedIndex = 2

在你的线后

tabBarController.viewControllers = vcs
另一答案

您可以通过以下方式随时更改RootViewController

let storyboard = UIStoryboard(name: "Main", bundle: Bundle.main)
let homeViewController = storyboard.instantiateViewController(withIdentifier: "HomeViewController") as! HomeViewController
UIApplication.shared.delegate?.window??.rootViewController = homeViewController

以上是关于如何将rootviewcontroller更改为堆栈中的第三个viewcontroller的主要内容,如果未能解决你的问题,请参考以下文章

在 applicaitonWillEnterForeground 中更改 rootViewController

如何在 Excel 中将逗号更改为小数?

如何在 AIX 终端中将大量 .pdf 更改为 .PDF 文件

ID:[...] 的 NSManagedObject 已失效

如何查看应用程序正在使用的当前堆大小?

更改 ViewController 并重新启动应用程序