How to add small red dot in UITabBarItem

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

    i test this question's answer. but not work on iPad. now i found that, when u add this on iPhone, tabBarItem left and right margin is 2, and each items margin is 4. Code as below:

        NSInteger barItemCount = self.tabBar.items.count;
    
        UITabBarItem *barItem = (UITabBarItem *)self.tabBar.items[index];
        CGFloat imageHalfWidth = barItem.image.size.width / 2.0;
    
        CGFloat barItemWidth = (BXS_WINDOW_WIDTH - barItemCount * 4) / barItemCount;
        CGFloat barItemMargin = 4;
        CGFloat redDotXOffset = barItemMargin / 2 + barItemMargin * index + barItemWidth * (index + 0.5);
    

    and iPad as below:

        barItemWidth = 76;
        barItemMargin = 34;
        redDotXOffset = (BXS_WINDOW_WIDTH - 76 * barItemCount - 34 * (barItemCount - 1)) / 2.0 + 76 * (index + 0.5) + 34 * index;
    

    Hope this is useful.

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

    Works both for iPad and iPhone. Be able to hide and calculate index automatically.

    Call self.setTabBarDotVisible(visible:true) if self is not an UITabBarController.

    Call self.setTabBarDotVisible(visible:true, index:2) if self is an UITabBarController.

    import UIKit
    
    public extension UIViewController {
    
    func setTabBarDotVisible(visible:Bool,index: Int? = nil) {
    
        let tabBarController:UITabBarController!
    
        if self is UITabBarController
        {
            tabBarController = self as! UITabBarController
        }
        else
        {
            if self.tabBarController == nil
            {
                return
            }
            tabBarController = self.tabBarController!
        }
    
        let indexFinal:Int
    
        if (index != nil)
        {
            indexFinal = index!
        }
        else
        {
            let index3 = tabBarController.viewControllers?.index(of: self)
    
            if index3 == nil
            {
                return;
            }
            else
            {
                 indexFinal = index3!
            }
    
        }
    
        guard let barItems = tabBarController.tabBar.items else
        {
            return
        }
    
        //
    
    
        let tag = 8888
    
        var tabBarItemView:UIView?
    
        for subview in tabBarController.tabBar.subviews {
    
            let className = String(describing: type(of: subview))
    
            guard className == "UITabBarButton" else {
                continue
            }
    
            var label:UILabel?
            var dotView:UIView?
    
            for subview2 in subview.subviews {
    
                if subview2.tag == tag {
                    dotView = subview2;
                }
                else if (subview2 is UILabel)
                {
                    label = subview2 as? UILabel
                }
    
            }
    
    
            if label?.text == barItems[indexFinal].title
            {
                dotView?.removeFromSuperview()
                tabBarItemView = subview;
                break;
            }
        }
    
        if (tabBarItemView == nil || !visible)
        {
            return
        }
    
    
    
        let barItemWidth = tabBarItemView!.bounds.width
    
        let x = barItemWidth * 0.5 + (barItems[indexFinal].selectedImage?.size.width ?? barItemWidth) / 2
        let y:CGFloat = 5
        let size:CGFloat = 10;
    
        let redDot = UIView(frame: CGRect(x: x, y: y, width: size, height: size))
    
        redDot.tag = tag
        redDot.backgroundColor = UIColor.red
        redDot.layer.cornerRadius = size/2
    
    
        tabBarItemView!.addSubview(redDot)
    }
    
    }
    
    0 讨论(0)
  • 2020-12-13 03:46

    I have figured out a hack solution.

    func addRedDotAtTabBarItemIndex(index: Int,dotRadius: CGFloat) {
    
        var tabBarButtons = [UIView]()
    
        // find the UITabBarButton instance.
        for subview in tabBarController!.tabBar.subviews.reverse() {
    
            if subview.isKindOfClass(NSClassFromString("UITabBarButton")) {
    
                tabBarButtons.append(subview as! UIView)
            }
        }
    
        if index >= tabBarButtons.count {
            println("out of bounds")
            return
        }
    
        let tabBar = tabBarButtons[index]
    
        var selectedImageWidth: CGFloat!
        var topMargin: CGFloat!
    
        for subview in tabBar.subviews {
    
            if subview.isKindOfClass(NSClassFromString("UITabBarSwappableImageView")) {
    
                selectedImageWidth = (subview as! UIView).frame.size.width
                topMargin = (subview as! UIView).frame.origin.y
            }
        }
    
        // remove existing red dot.
        for subview in tabBar.subviews {
    
            if subview.tag == 999 {
                subview.removeFromSuperview()
            }
        }
    
        let redDot = UIView(frame: CGRect(x: CGRectGetMidX(tabBar.bounds) + selectedImageWidth / 2 + dotRadius, y: topMargin, width: dotRadius * 2, height: dotRadius * 2))
    
        redDot.backgroundColor = UIColor.redColor()
        redDot.layer.cornerRadius = dotRadius   // half of the view's height.
        redDot.tag = 999
    
        tabBar.addSubview(redDot)
    
    }
    
    0 讨论(0)
  • 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)
    }
    
    0 讨论(0)
提交回复
热议问题