在Swift中更改标签栏徽章大小

时间:2018-01-05 08:45:05

标签: ios swift uitabbarcontroller uitabbaritem

如何更改标签栏徽章大小?

我将标签栏徽章值设置为位置

tabBarController?.tabBar.items?[4].badgeValue = "1"

但我无法更改红色圆圈标签栏徽章大小。

谢谢!

1 个答案:

答案 0 :(得分:7)

它正在运作

func addRedDotAtTabBarItemIndex(index: Int) {
    for subview in tabBarController!.tabBar.subviews {

        if let subview = subview as? UIView {

            if subview.tag == 1234 {
                subview.removeFromSuperview()
                break
            }
        }
    }

    let RedDotRadius: CGFloat = 5
    let RedDotDiameter = RedDotRadius * 2

    let TopMargin:CGFloat = 5

    let TabBarItemCount = CGFloat(self.tabBarController!.tabBar.items!.count)

    let screenSize = UIScreen.main.bounds
    let HalfItemWidth = (screenSize.width) / (TabBarItemCount * 2)

    let  xOffset = HalfItemWidth * CGFloat(index * 2 + 1)

    let imageHalfWidth: CGFloat = (self.tabBarController!.tabBar.items![index] as! UITabBarItem).selectedImage!.size.width / 2

    let redDot = UIView(frame: CGRect(x: xOffset + imageHalfWidth - 7, y: TopMargin, width: RedDotDiameter, height: RedDotDiameter))

    redDot.tag = 1234
    redDot.backgroundColor = UIColor.red
    redDot.layer.cornerRadius = RedDotRadius

        self.tabBarController?.tabBar.addSubview(redDot)

}