如何使用swift以编程方式在UITabBarController中添加选项卡?

时间:2014-11-10 18:26:24

标签: swift uiviewcontroller tabs uitabbarcontroller

如何从UIViewController扩展的任何类中以编程方式创建选项卡:

class DashboardTabBarController: UITabBarController {

    override func viewDidLoad() {
        //here

    }
 ...

}

3 个答案:

答案 0 :(得分:48)

UPDATE SWIFT 5

如何以编程方式创建UITabBarController的一个示例可能是这样的:

首先,我们创建UIViewControllers,它将成为标签栏界面每个标签的内容。对于这个例子,我们只创建一个非常简单的。

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

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

现在,UITabBarController

我们创建了要在标签栏中显示的UIViewControllers的新实例。然后我们为我们创建的每个实例创建一个图标,然后创建一个包含所有UIViewControllers的数组,指定标签栏界面的每个选项卡的内容。 数组中视图控制器的顺序对应于标签栏中的显示顺序

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;
    }
}

答案 1 :(得分:1)

如果您正在为viewcontrollers使用storyboard,那么您必须在tabbarcontroller类中这样写。

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

        }

    }
}

答案 2 :(得分:0)

    private lazy var tabbarViewController: UITabBarController = {
            let tabbarViewController = UITabBarController()
            tabbarViewController.setViewControllers([startVC,
                                                     offerVC,
                                                     benefitsVC,
                                                     shopVC,
                                                     recipesVC], animated: true)
            
            return tabbarViewController
        }()

window?.rootViewController = tabbarViewController