ios tabbar put text in the middle when no image

梦想的初衷 提交于 2019-11-30 17:48:43

Got it!

[tab.tabBarItem setTitlePositionAdjustment:UIOffsetMake(0, -10)]

A supplement, if you set multi viewControllers in tabBarController, you should use the following:

for (UITabBarItem* item in tabBarController.tabBar.items)
{
    [item setTitlePositionAdjustment:UIOffsetMake(0, -10)];
}

swift update.

func tabBarItem(title: String, imageName: String, selectedImageName: String, tagIndex: Int) -> UITabBarItem {
        let item = UITabBarItem(title: title,
                                image: UIImage(named: imageName),
                                selectedImage: UIImage(named: selectedImageName))
        item.titlePositionAdjustment = UIOffset(horizontal:0, vertical:-10)
        item.tag = tagIndex
        return item
    }

// For example

let window = UIWindow.window()
let vc = UIViewController()
vc.tabBarItem = tabBarItem(title: "More", imageName: "icon_more", selectedImageName: "icon_more", tagIndex: 1)


let mainTBC = UITabBarController()
    mainTBC.viewControllers = [vc]
 window?.rootViewController = mainTBC
 window?.makeKeyAndVisible()
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!