在 Swift 5 中以编程方式在 UITabBarController 中显示视图控制器
Displaying View Controller within a UITabBarController programatically in Swift 5
在我的应用程序中,当用户登录时,他们应该被带到欢迎屏幕,这是 TabBarController 的一部分。但出于某种原因,当我 运行 以下几行时,欢迎屏幕 (HomeScreenViewController) 显示底部没有 TabBar 导航按钮。
let homeViewController = self.storyboard?.instantiateViewController(identifier: Constants.Storyboard.homeViewController) as? HomeScreenViewController
self.view.window?.rootViewController = homeViewController
self.view.window?.makeKeyAndVisible()
受另一个 Whosebug post 的启发,我尝试了这个,但也没有用:
let homeController = self.storyboard?.instantiateViewController(identifier:Constants.Storyboard.homeViewController)
(TabBarController.currentInstance?.selectedViewController as?UINavigationController)?.pushViewController(homeController!, animated: true)
作为参考,我的 UITabBarController class 的名称是 TabBarController 并且可以通过
访问标识符
Constants.Storyboard.tabBarCont
谢谢!
我想你可以试试这个..
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
window = UIWindow(frame: UIScreen.main.bounds)
let tabBarController = UITabBarController()
let homeViewController = self.storyboard?.instantiateViewController(identifier: Constants.Storyboard.homeViewController) as? HomeScreenViewController
let anyOtherViewController = self.storyboard?.instantiateViewController(identifier: anyOtherViewController) as? AnyOtherViewController
homeViewController.tabBarItem = UITabBarItem(title: "Home", image: UIImage(named: "home_image"),tag: 1)
anyOtherViewController.tabBarItem = UITabBarItem(title: "Other",image:UIImage(named: "other") ,tag:2)
tabBarController.viewControllers = [homeViewController, anyOtherViewController]
window?.rootViewController = tabBarController
window?.makeKeyAndVisible()
return true
}
登录后这样使用:
let storyboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let centerViewController = storyboard.instantiateViewController(withIdentifier: "tabbar") as! TabBarController
centerViewController.selectedIndex = 0
centerViewController.modalPresentationStyle = UIModalPresentationStyle.fullScreen
let appDelegate = UIApplication.shared.delegate as! AppDelegate
if let window = UIApplication.shared.windows.first {
appDelegate.window = window
}
appDelegate.window!.rootViewController = centerViewController
self.present(centerViewController!, animated: false, completion: nil)
在我的应用程序中,当用户登录时,他们应该被带到欢迎屏幕,这是 TabBarController 的一部分。但出于某种原因,当我 运行 以下几行时,欢迎屏幕 (HomeScreenViewController) 显示底部没有 TabBar 导航按钮。
let homeViewController = self.storyboard?.instantiateViewController(identifier: Constants.Storyboard.homeViewController) as? HomeScreenViewController
self.view.window?.rootViewController = homeViewController
self.view.window?.makeKeyAndVisible()
受另一个 Whosebug post 的启发,我尝试了这个,但也没有用:
let homeController = self.storyboard?.instantiateViewController(identifier:Constants.Storyboard.homeViewController)
(TabBarController.currentInstance?.selectedViewController as?UINavigationController)?.pushViewController(homeController!, animated: true)
作为参考,我的 UITabBarController class 的名称是 TabBarController 并且可以通过
访问标识符Constants.Storyboard.tabBarCont
谢谢!
我想你可以试试这个..
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
window = UIWindow(frame: UIScreen.main.bounds)
let tabBarController = UITabBarController()
let homeViewController = self.storyboard?.instantiateViewController(identifier: Constants.Storyboard.homeViewController) as? HomeScreenViewController
let anyOtherViewController = self.storyboard?.instantiateViewController(identifier: anyOtherViewController) as? AnyOtherViewController
homeViewController.tabBarItem = UITabBarItem(title: "Home", image: UIImage(named: "home_image"),tag: 1)
anyOtherViewController.tabBarItem = UITabBarItem(title: "Other",image:UIImage(named: "other") ,tag:2)
tabBarController.viewControllers = [homeViewController, anyOtherViewController]
window?.rootViewController = tabBarController
window?.makeKeyAndVisible()
return true
}
登录后这样使用:
let storyboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let centerViewController = storyboard.instantiateViewController(withIdentifier: "tabbar") as! TabBarController
centerViewController.selectedIndex = 0
centerViewController.modalPresentationStyle = UIModalPresentationStyle.fullScreen
let appDelegate = UIApplication.shared.delegate as! AppDelegate
if let window = UIApplication.shared.windows.first {
appDelegate.window = window
}
appDelegate.window!.rootViewController = centerViewController
self.present(centerViewController!, animated: false, completion: nil)