How to add small red dot in UITabBarItem

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

    simple solution set space in storyboard tabbaritem badge value.

    if we add space below output you can get:

    0 讨论(0)
  • 2020-12-13 03:23

    If you want to avoid traversing subviews & potentially dangerous hacks in general, what I've done is set the badge's background colour to clear and used a styled bullet point to appear as a badge:

    tabBarItem.badgeValue = "●"
    tabBarItem.badgeColor = .clear
    tabBarItem.setBadgeTextAttributes([NSAttributedStringKey.foregroundColor.rawValue: UIColor.red], for: .normal)
    

    This seems more future-proof than the other answers.

    0 讨论(0)
  • 2020-12-13 03:25

    set the badgeValue for your desired UITabBarItem as follow:

        // for first tab
        (tabBarController!.tabBar.items!.first! as! UITabBarItem).badgeValue = "1"
    
        //for second tab
        (tabBarController!.tabBar.items![1] as! UITabBarItem).badgeValue = "2"
    
        // for last tab
        (tabBarController!.tabBar.items!.last! as! UITabBarItem).badgeValue = "final"
    

    for remove a badge from the UITabBarItem just assign nil

    (tabBarController!.tabBar.items!.first! as! UITabBarItem).badgeValue = nil
    

    you can get the output Like

    enter image description here

    for additional information please ref this link

    Choice --2

        var lbl : UILabel = UILabel(frame: CGRectMake(225, 5, 20, 20))
        lbl.layer.borderColor = UIColor.whiteColor().CGColor
        lbl.layer.borderWidth = 2
        lbl.layer.cornerRadius = lbl.bounds.size.height/2
        lbl.textAlignment = NSTextAlignment.Center
        lbl.layer.masksToBounds = true
        lbl.font = UIFont(name: hereaddyourFontName, size: 13)
        lbl.textColor = UIColor.whiteColor()
        lbl.backgroundColor = UIColor.redColor()
        lbl.text = "1"  //if you no need remove this
    
        // add subview to tabBarController?.tabBar
        self.tabBarController?.tabBar.addSubview(lbl)
    

    the output is

    enter image description here

    0 讨论(0)
  • 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
        }
    
    0 讨论(0)
  • 2020-12-13 03:32

    That is very simple in current iOS versions

    tabBarItem.badgeValue = " "
    

    it shows the red dot on the top of the tabbar item

    0 讨论(0)
  • 2020-12-13 03:32

    This it Swift 4 solution:

    1) Add BaseTabBar custom class to your project:

    import UIKit
    
    class BaseTabBar: UITabBar {
      static var dotColor: UIColor = UIColor.red
      static var dotSize: CGFloat = 4
      static var dotPositionX: CGFloat = 0.8
      static var dotPositionY: CGFloat = 0.2
    
      var dotMap = [Int: Bool]()
    
      func resetDots() {
        dotMap.removeAll()
      }
    
      func addDot(tabIndex: Int) {
        dotMap[tabIndex] = true
      }
    
      func removeDot(tabIndex: Int) {
        dotMap[tabIndex] = false
      }
    
      override func draw(_ rect: CGRect) {
        super.draw(rect)
        if let items = items {
          for i in 0..<items.count {
            let item = items[i]
            if let view = item.value(forKey: "view") as? UIView, let dotBoolean = dotMap[i], dotBoolean == true {
              let x = view.frame.origin.x + view.frame.width * BaseTabBar.dotPositionX
              let y = view.frame.origin.y + view.frame.height * BaseTabBar.dotPositionY
              let dotPath = UIBezierPath(ovalIn: CGRect(x: x, y: y, width: BaseTabBar.dotSize, height: BaseTabBar.dotSize))
              BaseTabBar.dotColor.setFill()
              dotPath.fill()
            }
          }
        }
      }
    
    }
    

    2) Change the custom class of UITabBar inside your UITabBarController to BaseTabBar.

    3) Manage the dots in the place where you can access the tabBarController

    func updateNotificationCount(count: Int) {
        if let tabBar = navigationController?.tabBarController?.tabBar as? BaseTabBar {
          if count > 0 {
            tabBar.addDot(tabIndex: 0)
          } else {
            tabBar.removeDot(tabIndex: 0)
          }
          tabBar.setNeedsDisplay()
        }
      }
    
    0 讨论(0)
提交回复
热议问题