I have built my app using new Xcode 9 beta for ios 11. I have found an issue with UITabBar where items are spread through the UITabBar and title is right aligned to the image. I have tried changing the code to get it to work but still not successful.
ios 10+
ios 11
I could change the position of title using tabBarItem.titlePositionAdjustment
But that is not my requirement as it should automatically come bellow the image itself. I tried setting tabbar.itemPositioning to UITabBarItemPositioningCentered
and also tried changing itemSpacing
and width
, but still did not work. Can someone help me understand why this happens and how to fix this? I want it to like ios 10+ version and images are taken from the left most corner of an iPad.
To avoid messing up any other traits is it not better to combine with the superclasses:
NOTE: This fix above seems to work quite nicely. Not sure how it will work in the future, but for now it's working quite well.
According to this WWDC talk, this is the new behavior for:
And if I'm reading correctly, this behavior cannot be changed:
I am maintaining a large iPad app written mostly in Objective-C that has survived several iOS releases. I ran into the situation where I needed the pre-iOS 11 tab bar appearance (with the icons above the titles instead of next to them) for a couple tab bars. My solution was to create a subclass of UITabBar that overrides the
traitCollection
method so that it always returns a horizontally-compact trait collection. This causes iOS 11 to display the titles below the icons for all of the tab bar buttons.In order to use this, set the custom class of the tab bars in the storyboard to this new subclass and change any outlets in the code that point to the tab bars to be of this new type (don't forget to import the header file below).
The .h file is pretty much empty in this case:
Here is the .m file with the implementation of the
traitCollection
method:In Addition to John's answer:
If you have more than 4 Tab Bar Items and don't want the "More" Button you have to take a different Size Class. And if you want the original centred layout of items you have to add another method like so:
Based on John C's answer, here is the Swift 3 version that can be used programmatically without need for Storyboard or subclassing:
Swift 4 version with a subclass that avoids extension/category naming collision:
If you use Interface Builder and storyboards, you can select the tab bar view in your
UITabBarController
scene and change its class toTabBar
in the Identity Inspector: