将 2 UITabBarItem 放置在屏幕的左右两侧,中间为 void

问题描述

有没有办法将 2 UITabBarItem 放在 UITabBarController 的左侧和右侧,并在中间留出空间,就好像有 2 个“未使用”的项目一样? itemPositioningitemWidthitemSpacing 似乎不是这样。

下面的代码只是将屏幕的整个宽度分成两个相等的部分,并将每个项目放置在其各自一半的中间:

class MainController: UITabBarController {
   override func viewDidLoad() {
      super.viewDidLoad()
                
      let vcOne = UIViewController()
      let vcTwo = UIViewController()
    
      let itemOne = UITabBarItem()
      itemOne.image = UIImage(named: "imgOne")

      let itemTwo = UITabBarItem()
      itemOne.image = UIImage(named: "imgTwo")

      vcOne.tabBarItem = itemOne
      vcTwo.tabBarItem = itemTwo
    
      tabBar.itemPositioning = .centered
      tabBar.itemWidth = UIScreen.main.bounds.width / 4
      tabBar.itemSpacing = UIScreen.main.bounds.width / 2
   }
}

我基本上是在寻找这样的东西:

enter image description here

我想我可以使用 UIEdgeInsets 但我正在寻找更可靠的东西来匹配同一个 UITabBar 中项目的位置,如果它包含 4 个项目而不是 2 个。

解决方法

您可以添加两个禁用的 UITabBarItem,中间没有图像或标题。