Search code examples
swifttvos

UITabBarController showing only the first tab


I am reasking this because the question in UITabbarController showing only first tab was not answered ... I don't understand why my UITabBarController is showing just the first tab .

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.

        self.window = UIWindow(frame: UIScreen.main.bounds)
        self.window?.backgroundColor = UIColor.darkGray
        self.window?.makeKeyAndVisible()
        let tab = UITabBarController()
        let v1 = VC1(nibName: "View1", bundle: nil)
        let v2 = VC2(nibName: "View2", bundle: nil)
        let v3 = VC3(nibName: "View3", bundle: nil)
        let myViews = [v1,v2,v3]
        tab.viewControllers = myViews
        self.window?.rootViewController = tab 
        return true
    }

}

VC1 Code:

class VC1: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        print("view 1 will load")
        self.title = "View 1"
        // Do any additional setup after loading the view.
    }

}

class VC2:

class VC1: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        print("view 2 will load")
        self.title = "View 2"
        // Do any additional setup after loading the view.
    }

}

VC3:

class VC1: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        print("view 3 will load")
        self.title = "View 3"
        // Do any additional setup after loading the view.
    }

}

I think this is a tested and proven way to create tabs but it doesn't seem to work, here's the result screenshot

Result Screenshot


Solution

  • I found a cleaner way to implement this , View Controller's title is a property and can be set right after it's instanciated

    So the updated code looks like :

    class AppDelegate: UIResponder, UIApplicationDelegate {
    
        var window: UIWindow?
    
    
        func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
            // Override point for customization after application launch.
    
            self.window = UIWindow(frame: UIScreen.main.bounds)
            self.window?.backgroundColor = UIColor.darkGray
            self.window?.makeKeyAndVisible()
            let tab = UITabBarController()
            let v1 = VC1(nibName: "View1", bundle: nil)
            let v2 = VC2(nibName: "View2", bundle: nil)
            let v3 = VC3(nibName: "View3", bundle: nil)
            v1.title = "View 1"
            v2.title = "View 2"
            v3.title = "View 3"
            let myViews = [v1,v2,v3]
            tab.setViewControllers(myViews, animated: false)
            self.window?.rootViewController = tab 
            return true
        }
    
    }
    

    and the screenshot is :

    3 Views TabBarController