How to add small red dot in UITabBarItem

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

提交回复
热议问题