2

I have 3 tab bar items, I want them to show like this in tab bar:

|                  |

|    1    2    3   |

|                  |

I see this property in InterfaceBuilder:

enter image description here

I have tried all cases, but it doesn't work, it shows something like this:

|                  |

|      1  2  3     |

|                  |
Twitter khuong291
  • 11,328
  • 15
  • 80
  • 116
  • 1
    use flexible space in between your bar items like flexiblespace-item1-flexiblespace-item2-flexiblespace-item3-flexiblespace – Saurabh Prajapati Feb 11 '17 at 08:44
  • Answered this question in another post. see solution here. [enter link description here](https://stackoverflow.com/a/44552862/2479910) – Gustavo_fringe Jun 14 '17 at 19:16

4 Answers4

4

Well, here is a universal solution, without any hard coded values.

In the viewDidLoad() of your UITabBarController, set

tabBar.itemPositioning = .fill

Dilpreet
  • 61
  • 4
1

Here is simplest solution:

If you have a sub TabBarController, add this code in viewDidLayoutSubviews, it works for me.

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()

    self.tabBar.itemSpacing = UIScreen.main.bounds.width / 6
}
Twitter khuong291
  • 11,328
  • 15
  • 80
  • 116
1

Here's the code for Objective-C.

-(void)viewDidLoad
{
    [super viewDidLoad];

    [self.tabBar setItemPositioning:UITabBarItemPositioningFill];
}

You can also set the value in the Storyboard on the Tab Bar

enter image description here

bvmobileapps
  • 177
  • 2
  • 7
1

The accepted answer requires tabBar.itemPositioning to be set in order for it to work. If the above answer does not work, try setting it to .centered. Thereafter, you should be able to programmatically set the item spacing.

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()

    self.tabBar.itemPositioning = .centered
    self.tabBar.itemSpacing = UIScreen.main.bounds.width / 6
}
drante
  • 129
  • 9