Search code examples
iosswiftuikitxcode11xcode11.3

Xcode 11.3, UInavigationController and UITabbarController facing issues


Brief: I have 4 controllers embedded in UINavigationController, who are then used as tabs in UITabbarController, (No storyboards and .xib's) through code.

Previously I was using Xcode 10.3, there were no issues but after I changed to 11.3 these issues came up.

Issues:

  1. when I tap on the tabs, viewWillAppear, and viewDidAppear on the controllers are not getting called.

  2. similar issue with navigation controller Xcode 11 UINavigationController Bar problems

  3. Pop back (pressing back button) from any controller after navigating to it is not getting animated.

Code TabbarController:

final class TabBarController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

        self.viewControllers = [
            TabBarItems.home.tabController,
            TabBarItems.search.tabController,
            TabBarItems.cart.tabController,
            TabBarItems.account.tabController
        ]

        self.tabBar.isTranslucent = false
    }
}

extension TabBarController {
    enum TabBarItems {
        case home
        case search
        case cart
        case account

        var title: String {
            switch self {

            case .home:
                return "Home"
            case .search:
                return "Search"
            case .cart:
                return "Bag"
            case .account:
                return "Account"
            }
        }

        var tabController: UINavigationController {
            switch self {

            case .home:
                return createNavController(viewController: HomeController(), title: self.title, imageName: Assets.home.rawValue)
            case .search:
                return createNavController(viewController: UIViewController(), title: self.title, imageName: Assets.search.rawValue)
            case .cart:
                return createNavController(viewController: BagController(), title: self.title, imageName: Assets.shoppingBag.rawValue)
            case .account:
                return createNavController(viewController: AccountController(), title: self.title, imageName: Assets.account.rawValue)
            }
        }

        private func createNavController(viewController: UIViewController, title: String, imageName: String) -> UINavigationController {
            let navController = UINavigationController(rootViewController: viewController)
            viewController.view.backgroundColor = .white
            navController.tabBarItem.title = title
            navController.tabBarItem.image = imageName.image
            return navController
        }
    }
}

Solution

  • The issue was with a project created in XCode 10.3 and trying to run it out of the box in XCode 11+.

    As prior to iOS 13/ XCode11, the apps starting point was AppDelegate but now in Xcode 11+, SceneDelegate shared some of the responsibility. i.e. the window configuration should be done in scene(:willConnectTo:) function.

    I forgot to add SceneDelegate.swift and Info.plist key for that.

    Thanks @DonMag for helping me out.