How add tabs programmatically in UITabBarController with swift?

前端 未结 2 1909
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-30 03:55

How to create programmatically tabs from any class extended by UIViewController:

class DashboardTabBarController: UITabBarController {

    override func vie         


        
2条回答
  •  死守一世寂寞
    2020-11-30 04:34

    If you are using storyboard for the viewcontrollers then you have to write like this in your tabbarcontroller class.

    class CustomTabbarController : UITabBarController {
    
        override func viewDidLoad() {
    
            super.viewDidLoad()
    
            let storyboard = UIStoryboard(name: "Main", bundle: nil)
    
            let firstViewController = FirstViewController()
            let navigationController = UINavigationController(rootViewController: firstViewController)
            navigationController.title = "First"
            navigationController.tabBarItem.image = UIImage.init(named: "map-icon-1")
    
           viewControllers = [navigationController]
    
            if let secondViewController = storyboard.instantiateViewController(withIdentifier: "SecondViewController") as? SecondViewController {
    
                let navgitaionController1 = UINavigationController(rootViewController: secondViewController)
                navgitaionController1.title = "Second"
                navgitaionController1.tabBarItem.image = UIImage.init(named: "second-icon-1")
                var array = self.viewControllers
                array?.append(navgitaionController1)
                self.viewControllers = array
    
            }
    
        }
    }
    

提交回复
热议问题