在 iOS Swift 中以编程方式创建 UITabBar?

问题描述 投票:0回答:1

我正在尝试以编程方式将唯一的 viewController 添加到 TabBarItems 并使用 IBOutlet 创建 UITabBar 控制器。

这是我创建 UITabBar 的代码

public class TabBarViewController: UIViewController, UITabBarDelegate {

   @IBOutlet weak var tabBarCnt: UITabBar!

   override public func viewDidLoad() {
    super.viewDidLoad()
     self.tabBarCnt.delegate = self
     createTabBarController()
   }

   func createTabBarController() {

        let firstVC = FirstViewController()
        firstVC.name = nameVal
        firstVC.tabBarItem = UITabBarItem.init(title: nil, image: UIImage(named: "ico_active", in: Bundle(for: TabBarViewController.self), compatibleWith: nil), tag: 0)
        firstVC.tabBarItem.imageInsets = UIEdgeInsets(top: 5, left: 0, bottom: -5, right: 0);

        let secondVC = SecondViewController()
        firstVC.name = nameVal
        firstVC.tabBarItem = UITabBarItem.init(title: nil, image: UIImage(named: "ico_active", in: Bundle(for: TabBarViewController.self), compatibleWith: nil), tag: 0)
        firstVC.tabBarItem.imageInsets = UIEdgeInsets(top: 5, left: 0, bottom: -5, right: 0);

        let thirdVC = ThirdViewController()
        thirdVC.name = nameVal
        thirdVC.tabBarItem = UITabBarItem.init(title: nil, image: UIImage(named: "ico_active", in: Bundle(for: TabBarViewController.self), compatibleWith: nil), tag: 0)
        thirdVC.tabBarItem.imageInsets = UIEdgeInsets(top: 5, left: 0, bottom: -5, right: 0);


        tabBarCnt.items = [firstVC, secondVC, thirdVC]
        self.view.bringSubviewToFront(tabBarCnt)
    }

}

它不显示任何视图控制器。

如何根据我的代码创建 UITabBar?

ios swift uiviewcontroller uitabbarcontroller uitabbar
1个回答
3
投票

A

UITabBar
不显示任何视图控制器。它只显示选项卡。

您很可能应该使用

UITabBarController
,而不是独立的
UITabBar
。更改您的
TabBarViewController
类以扩展
UITabBarController
而不是
UIViewController
并消除
UITabBar
插座。

public class TabBarViewController: UITabBarController {
    override public func viewDidLoad() {
        super.viewDidLoad()

        setupViewControllers()
    }

    func setupViewControllers() {
        let firstVC = FirstViewController()
        firstVC.name = nameVal
        firstVC.tabBarItem.image = UIImage(named: "ico_active", in: Bundle(for: TabBarViewController.self), compatibleWith: nil)
        firstVC.tabBarItem.imageInsets = UIEdgeInsets(top: 5, left: 0, bottom: -5, right: 0);

        let secondVC = SecondViewController()
        secondVC.name = nameVal
        secondVC.tabBarItem.image = UIImage(named: "ico_active", in: Bundle(for: TabBarViewController.self), compatibleWith: nil)
        secondVC.tabBarItem.imageInsets = UIEdgeInsets(top: 5, left: 0, bottom: -5, right: 0);

        let thirdVC = ThirdViewController()
        thirdVC.name = nameVal
        thirdVC.tabBarItem.image = UIImage(named: "ico_active", in: Bundle(for: TabBarViewController.self), compatibleWith: nil)
        thirdVC.tabBarItem.imageInsets = UIEdgeInsets(top: 5, left: 0, bottom: -5, right: 0);

        viewControllers = [firstVC, secondVC, thirdVC]
    }
}

请注意,您不需要为每个视图控制器创建

UITabBarItem
的新实例。

您还可以向视图控制器添加扩展来处理选项卡栏控制器委托方法。类似于以下内容:

extension TabBarViewController: UITabBarControllerDelegate {
    func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
        // Handle the user selecting a different tab
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.