【问题标题】:How to center TabBarItem - Swift如何居中 TabBarItem - Swift
【发布时间】:2020-03-24 14:46:34
【问题描述】:

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

我的红圈代码:

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 居中,或者如何计算该图标的位置?

【问题讨论】:

  • 在什么中间的位置?
  • 标签的。它应该在红色圆圈的上方。
  • 是的,我试过了。它不起作用。如果不把它放在中心,你只需要计算图标的位置,但是如何计算呢?
  • 可能是 UITabBarItem 的子类并自己绘制。

标签: ios swift xcode tabbar


【解决方案1】:

您可以尝试使用imageInsetsUITabBarItem,如下代码:

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

【讨论】:

  • for item in items! 是不好的做法,可能会导致崩溃。最好使用if let items = tabBar.items { for item in items { ... } }
猜你喜欢
  • 1970-01-01
  • 2018-02-19
  • 2016-03-28
  • 1970-01-01
  • 1970-01-01
  • 2016-04-11
  • 1970-01-01
  • 1970-01-01
  • 2015-02-09
相关资源
最近更新 更多