如何在 UITabBarController 的特定索引中添加 TabBarItem?

How to add a TabBarItem in specific index of UITabBarController?

如何在UITabBarController的特定索引中添加一个TabBarItem?

我可以使用

添加新的标签栏项目

[self.tabBarItems addObject:nav];

它总是在最后添加。我需要在特定索引位置添加标签栏项目。我该怎么做?

这里是插入新视图标签栏项目的方法:

import UIKit

class ViewController: UIViewController {
    
    // button and textfields are defined in the storyboard
    @IBOutlet var button: UIButton!
    var tabbar: UITabBarController?
    @IBOutlet var label: UITextField!
    @IBOutlet var image: UITextField!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        label.text = "Four"
        image.text = "info.circle.fill"
        tabbar = self.tabBarController
    }

    @IBAction func addVC(button: UIButton) {
        // remove keyboard
        label.resignFirstResponder()
        image.resignFirstResponder()
        // check that everything is set
        if var tbVC = tabbar?.viewControllers,
           let title = label.text,
           let imageName = image.text {
            // create new view controller
            let newVC = UIViewController()
            
            // vreate the tab bar item for the new view controller
            let tabBarItem = UITabBarItem(title: title,
                                          image: UIImage(systemName: imageName),
                                          tag: tbVC.count + 1)
            newVC.tabBarItem = tabBarItem
            
            // insert new tab in tab bar view controller
            tbVC.insert(newVC, at: 2)
            
            // update the teb bar controller
            tabbar?.setViewControllers(tbVC, animated: true)
        }
    }

}