How to add small red dot in UITabBarItem

后端 未结 10 2327
一整个雨季
一整个雨季 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:27

    I added 5 tab bar indexes and add the dot points according to the notification occurs. First, create Dots view array.

    var Dots = [UIView](repeating: UIView(), count: 5)
    
    func addRedDotAtTabBarItemIndex(index: Int) {
    
        if  self.Dots[index].tag != index {
    
            let RedDotRadius: CGFloat = 7
            let RedDotDiameter = RedDotRadius
    
            let TopMargin:CGFloat = 2
    
            let tabSize = self.tabBarController.view.frame.width / CGFloat(5)
    
            let  xPosition = tabSize * CGFloat(index - 1)
    
            let tabHalfWidth: CGFloat = tabSize / 2
    
            self.Dots[index] =  UIView(frame: CGRect(x: xPosition + tabHalfWidth - 2 , y: TopMargin, width: RedDotDiameter, height: RedDotDiameter))
    
            self.Dots[index].tag = index
            self.Dots[index].backgroundColor = UIColor.red
            self.Dots[index].layer.cornerRadius = RedDotRadius
    
            self.tabBarController.tabBar.addSubview(self.Dots[index])
    
        }
    }
    

    If you want to remove the dot from selected index, use this code:

    func removeRedDotAtTabBarItemIndex(index: Int) {
    
            self.Dots[index].removeFromSuperview()
            self.Dots[index].tag = 0
        }
    

提交回复
热议问题