How to add small red dot in UITabBarItem

后端 未结 10 2329
一整个雨季
一整个雨季 2020-12-13 02:44

How to add red dot on the top right side of the UITabBarItem. \"enter

I h

10条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-13 03:47

    you can try this method:

    func addRedDotAtTabBarItemIndex(index: Int) {
    
        for subview in tabBarController!.tabBar.subviews {
    
            if let subview = subview as? UIView {
    
                if subview.tag == 1314 {
                    subview.removeFromSuperview()
                    break
                }
            }
        }
        let RedDotRadius: CGFloat = 5
        let RedDotDiameter = RedDotRadius * 2
    
        let TopMargin:CGFloat = 5
    
        let TabBarItemCount = CGFloat(self.tabBarController!.tabBar.items!.count)
    
        let HalfItemWidth = CGRectGetWidth(view.bounds) / (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, y: TopMargin, width: RedDotDiameter, height: RedDotDiameter))
    
        redDot.tag = 1314
        redDot.backgroundColor = UIColor.redColor()
        redDot.layer.cornerRadius = RedDotRadius
    
    
        self.tabBarController?.tabBar.addSubview(redDot)
    }
    

提交回复
热议问题