【问题标题】:Change tab bar badge size in Swift在 Swift 中更改标签栏徽章大小
【发布时间】:2018-01-05 08:45:04
【问题描述】:

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

我用位置设置标签栏徽章值

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

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

谢谢!

【问题讨论】:

  • 我想尝试使用 setValerForKey(:) 方法
  • 这个方法不能改变红色圆圈标签栏徽章的大小。
  • 使用自定义标签栏
  • 除非您想创建自己的自定义标签栏,否则您不会。做到这一点的唯一方法是在视图层次结构中四处寻找,或者做一些事情,比如添加你自己的自定义视图。这两种情况都可能导致您的应用被拒绝。一般来说,与标准控制作斗争并不是一个好主意。

标签: ios swift uitabbarcontroller uitabbaritem


【解决方案1】:

它正在工作

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)

}

【讨论】:

  • 有用的代码。谢啦。像我自己写的那样把它写成一个扩展可能是个好主意。
  • 它只是添加到一个标签,我想添加到许多标签栏项目
  • 这个chanef的代码
猜你喜欢
  • 2018-10-31
  • 2013-05-07
  • 2017-09-22
  • 1970-01-01
  • 1970-01-01
  • 2015-01-23
  • 1970-01-01
  • 1970-01-01
  • 2021-02-14
相关资源
最近更新 更多