![](/img/trans.png)
[英]How to set a UITabBarController as rootViewController from Storyboard
[英]Set UITabBarController as rootViewController
我有一個UITabBarController
,如果用戶會話仍然處於活動狀態,我想顯示此屏幕而不是登錄屏幕。
我的UITabBarController
有 3 個ViewControllers
,問題是我在底部看不到TabBar
鏈接,而且我無法導航。
如果沒有以下代碼,一切正常。 我的意思是登錄后我可以導航。
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
FirebaseApp.configure()
if Auth.auth().currentUser != nil {
window = UIWindow(frame: UIScreen.main.bounds)
window?.makeKeyAndVisible()
window?.rootViewController = HomeTabBarController()
}
return true
}
我也試過下面的代碼來設置rootViewController
但它是同樣的問題。
當我嘗試將其他視圖控制器之一設置為根(子項之一)時,標簽欄根本不顯示
var rootView: MyRootViewController = MyRootViewController()
if let window = self.window{
window.rootViewController = rootView
}
我在這里做錯了什么?
我遇到了同樣的問題,我遇到了你的帖子,你的代碼的問題是HomeTabBarController()
正在創建一個全新的 TabBarController 所以要修復它,請嘗試我使用的以下方法。
if Auth.auth().currentUser != nil {
print("******************************User Present******************************")
self.window = UIWindow(frame: UIScreen.main.bounds)
let storyboard = UIStoryboard(name: "Main", bundle: nil)
// create view controllers from storyboard
// Interface Builder -> Identitiy Inspector -> Storyboard ID
// Set up the Tab Bar Controller to have two tabs
let tabBarController = storyboard.instantiateViewController(withIdentifier: "HomeTabBarController") as! HomeTabBarController
// Make the Tab Bar Controller the root view controller
window?.rootViewController = tabBarController
window?.makeKeyAndVisible()
}
編輯確保將標識符添加到您的 TabBarController
// Interface Builder -> Identitiy Inspector -> Storyboard ID
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
//// this code changes the initial point of aap///////
window = UIWindow(frame: UIScreen.main.bounds)
let nav = UINavigationController()
let myview = SettingTabbar()
nav.viewControllers = [myview]
window?.rootViewController = nav
window?.makeKeyAndVisible()
return true
}
And Function SettingTabbar is:
func SettingTabbar()->(UITabBarController)
{
//Setting TabBar
let tabbar = UITabBarController()
//Designing Tabbar Item Images
let table = UITabBarItem(title: nil, image:UIImage(named: "002-list") , tag: 0)
let collection = UITabBarItem(title: nil, image: UIImage(named: "001-collect"), tag: 1)
let insert = UITabBarItem(title: nil, image: UIImage(named: "add"), tag: 2)
//Getting TabBar ViewControllers
let TableView = newViewController()
let CollectionView = PersonCollectionViewController()
let InsertRec = nextViewController()
//Setting ViewControllers on TabBar Items
TableView.tabBarItem = table
CollectionView.tabBarItem = collection
InsertRec.tabBarItem = insert
let controllers = [TableView,CollectionView,InsertRec]
tabbar.viewControllers = controllers
tabbar.viewControllers = controllers.map{UINavigationController(rootViewController: $0)}
//Setting Title
tabbar.navigationItem.title = "Person Record"
return tabbar
}
問題是這一行:
window?.rootViewController = HomeTabBarController()
那是錯誤的 HomeTabBarController。 這是一個全新的 HomeTabBarController,沒有孩子。 您需要獲取故事板中的 HomeTabBarController。
我終於找到了解決方案:正如@matt 建議的那樣,我必須獲取故事板中的 HomeTabBarController。
window = UIWindow(frame: UIScreen.main.bounds)
let storyboard = UIStoryboard.init(name: "Main", bundle: nil)
// controller identifier sets up in storyboard utilities
// panel (on the right), it called Storyboard ID
let viewController = storyboard.instantiateViewController(withIdentifier: "HomeTabBarController") as! HomeTabBarController
self.window?.rootViewController = viewController
self.window?.makeKeyAndVisible()
window?.makeKeyAndVisible()
window?.rootViewController = viewController
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.