I am trying to create a tab bar controller with a navigation controller. I am trying to achieve this programmatically.
Here's the willConnectTo method in my Scene delegate:
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
// Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
// If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
// This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
guard let windowScene = (scene as? UIWindowScene) else { return }
let window = UIWindow(windowScene: windowScene)
// Create the tab bar controller
let tabBarController = UITabBarController()
let vc1 = UINavigationController(rootViewController: MoviesScreen())
let vc2 = UINavigationController(rootViewController: FavouritesScreen())
vc1.tabBarItem.image = UIImage(systemName: "film")
vc1.tabBarItem.selectedImage = UIImage(systemName: "film.fill")
vc2.tabBarItem.image = UIImage(systemName: "heart")
vc2.tabBarItem.selectedImage = UIImage(systemName: "heart.fill")
tabBarController.setViewControllers([vc1, vc2], animated: false)
window.rootViewController = tabBarController
self.window = window
window.makeKeyAndVisible()
}
And here are the 2 view controllers:
class MoviesScreen: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
title = "Movies"
view.backgroundColor = .red
}
}
class FavouritesScreen: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
title = "Favourites"
view.backgroundColor = .yellow
}
}
The screenshot below shows what the app looks like when I run it:

But when I click on the tab bar item, the item shows up:

As you can see in the first screenshot, the second tab bar item is not visible. I tried the existing fixes I could find on the internet but no luck.