demon9733
demon9733

Reputation: 854

How to change the width of UITabBarItem?

How to change the width of UITabBarItem?

I have an item with the title, that is wider than default item width. How can I fix it?

Upvotes: 10

Views: 11792

Answers (5)

Gurjinder Singh
Gurjinder Singh

Reputation: 10329

Swift 5

UITabBar().itemWidth = self.window!.frame.size.width/5

Upvotes: 0

rad182
rad182

Reputation: 85

Found easier solution for swift if you subclass the UITabBarController this will autofill the tab bar items evenly

self.tabBar.itemPositioning = .fill

Upvotes: 1

jomafer
jomafer

Reputation: 2725

Solution for iOS7 and greater versions:

A new property has been introduced for UITabBar: itemWidth.

Set the itemWidth to a positive value to be used as the width for tab bar items when they are positioned as a centered group (as opposed to filling the tab bar). Default of 0 or values less than 0 will be interpreted as a system-defined width.

@property(nonatomic) CGFloat itemWidth NS_AVAILABLE_IOS(7_0) UI_APPEARANCE_SELECTOR;

So you can do this in your AppDelegate:

[[UITabBar appearance] setItemWidth:WIDTH];

If you want to fill the whole UITabBar:

[[UITabBar appearance] setItemWidth:self.window.frame.size.width/NUMBER_OF_ITEMS];

How to fill the UITabBar

Upvotes: 19

MrTJ
MrTJ

Reputation: 13202

You can change the spacing (and width) of the tab bar items by subclassing UITabBar and overriding its layoutSubviews method. You will find all tab bar buttons in the self.subViews array. Their class is the non-public UITabBarButton but they inherit from UIControl so you can identify all tab bar buttons checking if they are kind of UIControl class. Then all you need is to change the frame of the tab bar buttons.

Upvotes: 3

rich
rich

Reputation: 2136

I just searched through the documentation and saw no method for adjusting the UITabBarItem's width.

A possible solution would be to place a view within the tab bar controller to load a custom UITabBarItem, that is the proper width.

Upvotes: 1

Related Questions