对于 tabBar 中的每个 UITabBarItem
都使用此代码。这会使您的图像在垂直方向上居中。
tabOne.tabBarItem.imageInsets = UIEdgeInsets.init(top: 5,left: 0,bottom: -5,right: 0)
更新,附例:
let nvOneViewController = MYRNavigationController()
let tabOne = MYRHomeViewController()
let tabOneBarItem = UITabBarItem(title: "", image: UIImage(named: "icnHome"), selectedImage: UIImage(named: "icnHomeOn"))
tabOne.tabBarItem = tabOneBarItem
tabOne.tabBarItem.imageInsets = UIEdgeInsets.init(top: 5,left: 0,bottom: -5,right: 0)
nvOneViewController.setViewControllers([tabOne], animated: false)
tabOne
is the viewController
- Vicky_VigneshUITabBarItem
代码,我可以帮助您! - Vicky_Vignesh在iOS 13上,图标似乎已经垂直居中,无需任何自定义代码。在iOS 13之前,我必须像@Vicky_Vignesh提到的那样添加图像插入。
UITabBarController
的API。 - Sandeep Bhandari