在我們的項(xiàng)目中,除非是只有一個控制器的App,否則,通常需要對這些控制器進(jìn)行管理。為了方便對這些控制器進(jìn)行管理,iOS提供了2種特殊的控制器:1.UITabBarController 2.UINavigationController.關(guān)于這兩個特殊控制器的一般用法,可以查看這篇文章:iOS多控制器之UINavigationController&UITableBarController - 簡書。
我們將要介紹的是如何同時使用這兩種特殊控制器,我們先看看同時使用這兩個控制器時候它們之間的關(guān)系:

image
可以看到TabbarView位于UIWindow之上,而NavigationView位于TabbarView之上,最后我們的主要視圖在最上方。這里的關(guān)系我們在SceneDelegate.swift里進(jìn)行設(shè)置,而在此之前我們要在targets中將Main Interface設(shè)置為空,不使用默認(rèn)的故事板,否則會出現(xiàn)黑屏:

image
設(shè)置完后開始在SceneDelegate.swift編寫代碼:
guard let _ = (scene as? UIWindowScene) else { return }
self.window = UIWindow(windowScene: scene as! UIWindowScene)
let tab = UITabBarController.init()
let uikitVC = UIStoryboard.init(name: "uiKit", bundle: nil).instantiateViewController(identifier: "uiVC")
let uiNav = UINavigationController(rootViewController: uikitVC)
uiNav.title = "uiKit"
let arVC = UIStoryboard.init(name: "AR", bundle: nil).instantiateViewController(identifier: "ARVC")
let arN = UINavigationController(rootViewController: arVC)
arN.title = "AR"
tab.viewControllers = [uiNav, arN]
self.window?.rootViewController = tab
self.window?.makeKeyAndVisible()
當(dāng)我們要在navigation所屬控制器內(nèi)部進(jìn)行頁面跳轉(zhuǎn)時,我們使用push的方法進(jìn)行壓棧,同時若我們想在push后的VC中底部的tabbaritem隱藏時,我們還要進(jìn)行相應(yīng)的設(shè)置:
let photoVC = UIStoryboard.init(name: "uiKit", bundle: nil).instantiateViewController(identifier: "photoVC")
photoVC.hidesBottomBarWhenPushed = true// navigation push到另一個視圖時候底部tabbaritem不顯示
self.navigationController?.pushViewController(photoVC, animated: true)
至此就基本完成兩種控制器的混合編程了!