1

tab bar

I cannot solve this issue, related to the position of the title of every single tab bar item in a UITabBar. I tried with this:

extension UITabBar {
    override open var traitCollection: UITraitCollection {
        if UIDevice.current.userInterfaceIdiom == .pad {
            return UITraitCollection(horizontalSizeClass: .compact)
        }
        return super.traitCollection
    }
}

With no success. How can I solve this misalignment?

Andrea Mario Lufino
  • 7,921
  • 12
  • 47
  • 78
  • For iPad, please refer to this answer : https://stackoverflow.com/questions/69217563/uitabbarappearance-does-not-work-on-ios15-ipad-title-color – alsub Feb 20 '23 at 17:11

1 Answers1

4

Check if this is worked, change values in UIOffset per what position you want.

if #available(iOS 13, *) {
    let appearance = UITabBarAppearance()
    
    // set padding between tabbar item title and image
    appearance.stackedLayoutAppearance.selected.titlePositionAdjustment = UIOffset(horizontal: 0, vertical: 4)
    appearance.stackedLayoutAppearance.normal.titlePositionAdjustment = UIOffset(horizontal: 0, vertical: 4)
    
    self.tabBar.standardAppearance = appearance
} else {
    // set padding between tabbar item title and image
    UITabBarItem.appearance().titlePositionAdjustment = UIOffset(horizontal: 0, vertical: 4)
}
Zhou Haibo
  • 1,681
  • 1
  • 12
  • 32