Twitter khuong291
Twitter khuong291

Reputation: 11672

How to spacing tab bar items equally in tab bar

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     |

|                  |

Upvotes: 2

Views: 11699

Answers (4)

drante
drante

Reputation: 139

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
}

Upvotes: 1

bvmobileapps
bvmobileapps

Reputation: 175

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

Upvotes: 1

Dilpreet
Dilpreet

Reputation: 61

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

In the viewDidLoad() of your UITabBarController, set

tabBar.itemPositioning = .fill

Upvotes: 4

Twitter khuong291
Twitter khuong291

Reputation: 11672

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
}

Upvotes: 1

Related Questions