How add tabs programmatically in UITabBarController with swift?

北城以北 提交于 2019-12-17 09:43:09

问题


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

class DashboardTabBarController: UITabBarController {

    override func viewDidLoad() {
        //here

    }
 ...

}

回答1:


UPDATE SWIFT 5

One example of how to create an UITabBarController programmatically could be like this:

First we create the UIViewControllers that will be the content for each tab of the tab bar interface. For this example we only create one very simple.

class Item1ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = UIColor.green
        self.title = "item1"
        print("item 1 loaded")
    }
}

Now, the UITabBarController:

We create the new instances of the UIViewControllers that we want to display in the tab bar. Then we create an icon for each instance we have created and then we create an array that contains all UIViewControllers that specify the content for each tab of the tab bar interface. The order of the view controllers in the array corresponds to the display order in the tab bar.

class DashboardTabBarController: UITabBarController, UITabBarControllerDelegate {
    override func viewDidLoad() {
        super.viewDidLoad()
        delegate = self
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        let item1 = Item1ViewController()
        let icon1 = UITabBarItem(title: "Title", image: UIImage(named: "someImage.png"), selectedImage: UIImage(named: "otherImage.png"))
        item1.tabBarItem = icon1
        let controllers = [item1]  //array of the root view controllers displayed by the tab bar interface
        self.viewControllers = controllers
    }

    //Delegate methods
    func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
        print("Should select viewController: \(viewController.title ?? "") ?")
        return true;
    }
}



回答2:


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

        }

    }
}


来源:https://stackoverflow.com/questions/26850411/how-add-tabs-programmatically-in-uitabbarcontroller-with-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!