2
votes

My goal is to display a tab view controller that manages multiple tabs that consist of navigation controllers containing view controllers.

I set the tab view controller BaseTabBarController as window my root view controller in AppDelegate. My custom tab view controller looks like this:

class BaseTabBarController: ESTabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .red

        let v1 = BaseNavigationController(rootViewController: SubscriptionsController())
        let v2 = BaseNavigationController(rootViewController: SubscriptionsController())

        v1.tabBarItem = ESTabBarItem(title: "Home", image: #imageLiteral(resourceName: "tab_bar_home"), selectedImage: #imageLiteral(resourceName: "tab_bar_home"))
        v2.tabBarItem = ESTabBarItem(title: "Home", image: #imageLiteral(resourceName: "tab_bar_home"), selectedImage: #imageLiteral(resourceName: "tab_bar_home"))

        self.viewControllers = [v1, v2]
        self.hidesBottomBarWhenPushed = true
    }
}

My custom navigation controller class is an empty subclass of a navigation controller.

The problem is that the app displays a tab bar for a fraction of a second, and immediately turns into a black screen (console message: "Presenting view controllers on detached view controllers is discouraged"). What did I do wrong?

1

1 Answers

1
votes

there has got to be something wrong with some other parts of your code. when i take your code and use it like this everything works as expected:

class BaseTabBarController: UITabBarController {
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .red

        let v1 = UINavigationController(rootViewController: UIViewController())
        let v2 = UINavigationController(rootViewController: UIViewController())

        v1.tabBarItem = UITabBarItem(title: "Home", image: nil, selectedImage: nil)
        v2.tabBarItem = UITabBarItem(title: "Home", image: nil, selectedImage: nil)

        self.viewControllers = [v1, v2]
        self.hidesBottomBarWhenPushed = true
    }
}

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.
    window = UIWindow(frame: UIScreen.main.bounds)
    window?.rootViewController = BaseTabBarController()
    window?.makeKeyAndVisible()

    return true
}