Swift中UITabBar按钮的垂直居中对齐

4

这是我想要做的:

Screen Shot

这张截图是来自早期版本的iOS设备,在该版本中UITabBar按钮并不处于中心对齐。
下面你可以查看我的当前的标签栏。

Screen Shot

这是我通过主.storyboard添加的UITabBar按钮。

如何将所有的 UITabBar 按钮设置为垂直居中对齐?

请查看 https://github.com/eggswift/ESTabBarController (ESTabBar) 我已经使用过它,它的表现相当不错,并且具有类似于 UITabBarController 的API。 - Sandeep Bhandari
3个回答

5
你还可以通过Main.StoryBoard设置图像的插入位置:


4

对于 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_Vignesh
@ShamDhiman,请查看更新后的答案,我已经添加了一个例子。 - Vicky_Vignesh
可以,但是你的代码没有给我输出。我正在使用自定义tabbar类和自定义导航类,但当我尝试使用你的代码时,它们没有给出任何输出。 - user10632910
@ShamDhiman 请添加您的 UITabBarItem 代码,我可以帮助您! - Vicky_Vignesh
我正在使用StoryBoard添加uitabbarItem。 - user10632910

2

在iOS 13上,图标似乎已经垂直居中,无需任何自定义代码。在iOS 13之前,我必须像@Vicky_Vignesh提到的那样添加图像插入。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接