Отображение View Controller внутри UITabBarController программно в Swift 5 - PullRequest
0 голосов
/ 27 марта 2020

В моем приложении, когда пользователь входит в систему, они должны быть выведены на экран приветствия, который является частью 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

Спасибо!

Ответы [ 2 ]

0 голосов
/ 27 марта 2020

Используйте как после входа в систему:

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)
0 голосов
/ 27 марта 2020

Я думаю, вы можете попробовать это ..

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
     }
...