Search code examples
iosswiftuitabbarcontrolleruitabbaritem

Position 2 UITabBarItem to left and right of screen, with void in the middle


Is there a way to position 2 UITabBarItem to the left and right of a UITabBarController and leave space in the middle as if there was 2 "unused" items? itemPositioning, itemWidth and itemSpacing don't seem to work that way.

The code below just splits the entire width of the screen in two equal parts, and position each item in the middle of its respective half:

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

I'm basically looking for something like this:

enter image description here

I guess I could use UIEdgeInsets but I'm looking for something more reliable to match the position of items in the same UITabBar, should it hold 4 items instead of 2.


Solution

  • You could add two disabled UITabBarItems with no image or title in the middle.