【问题标题】:Displaying View Controller within a UITabBarController programatically in Swift 5在 Swift 5 中以编程方式在 UITabBarController 中显示视图控制器
【发布时间】:2020-03-27 02:07:12
【问题描述】:

在我的应用程序中,当用户登录时,他们应该被带到一个欢迎屏幕,它是 TabBarController 的一部分。但由于某种原因,当我运行以下几行时,欢迎屏幕 (HomeScreenViewController) 显示底部没有 TabBar 导航按钮。

let homeViewController = self.storyboard?.instantiateViewController(identifier: Constants.Storyboard.homeViewController) as? HomeScreenViewController
self.view.window?.rootViewController = homeViewController
self.view.window?.makeKeyAndVisible()

受另一个 StackOverflow 帖子的启发,我尝试了这个,但也没有用:

let homeController = self.storyboard?.instantiateViewController(identifier:Constants.Storyboard.homeViewController)
(TabBarController.currentInstance?.selectedViewController as?UINavigationController)?.pushViewController(homeController!, animated: true)

作为参考,我的UITabBarController类的名字是TabBarController,标识符可以通过

Constants.Storyboard.tabBarCont

谢谢!

【问题讨论】:

    标签: ios swift uitabbarcontroller swift5 xcode11


    【解决方案1】:

    我想你可以试试这个..

    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
         }
    

    【讨论】:

      【解决方案2】:

      登录后这样使用:

      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)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-08-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-11-08
        • 2019-03-16
        • 1970-01-01
        相关资源
        最近更新 更多