如何居中TabBarItem-Swift

时间:2019-11-29 00:15:37

标签: ios swift xcode tabbar

如何将TabBarIcon放置在中间?它应该放在这个红色圆圈的顶部。 Screenshot

我的红圈代码:

let numberOfItems = CGFloat(tabBar.items!.count)
let tabBarItemSize = CGSize(width: tabBar.frame.width / numberOfItems, height: tabBar.frame.height)
let itemBackgroundView = UIView(frame: CGRect(x: tabBarItemSize.width / 2, y: 35, width: 6, height: 6))
itemBackgroundView.backgroundColor = .red
itemBackgroundView.layer.cornerRadius = 3
tabBar.addSubview(itemBackgroundView)

如何将TabBarIcon居中放置,或者如何计算该图标的位置?

1 个答案:

答案 0 :(得分:0)

您可以尝试使用imageInsets中的UITabBarItem,如下代码:

let items = tabBarCnt.tabBar.items
for item in items!
{
   item.imageInsets = UIEdgeInsets(top: 5, left: 0, bottom: -5, right: 0)
}