swift之Tabbar小红点实现

952 阅读1分钟

最近做了tabbar小红点功能, 记录一下

基于UITabBar的小红点

UITabBar+Extenstion.swift

import Foundation

let lxfFlag: Int = 100


extension UITabBar {

    // MARK: - 显示小红点

    func showBadgOn(index: TabType = .message, tabbarItemNums: CGFloat = 3.0) {

        let itemIndex = index.rawValue

        // 移除之前的小红点

        removeBadgeOn(index: index)

        // 创建小红点

        let bageView = UIView()

        bageView.tag = itemIndex + lxfFlag

        bageView.layer.cornerRadius = 3

        bageView.backgroundColor = UIColor.red

        let tabFrame = frame

        // 确定小红点的位置

        let percentX: CGFloat = (CGFloat(itemIndex) + 0.55) / tabbarItemNums

        let x: CGFloat = CGFloat(ceilf(Float(percentX * tabFrame.size.width)))

        let y: CGFloat = CGFloat(ceilf(Float(0.08 * tabFrame.size.height)))

        bageView.frame = CGRect(x: x, y: y, width: 6, height: 6)

        addSubview(bageView)

    }


    // MARK: - 隐藏小红点

    func hideBadg(index: TabType = .message) {

        removeBadgeOn(index: index)

    }


    // MARK: - 移除小红点

    fileprivate func removeBadgeOn(index itemIndex: TabType) {

        // 按照tag值进行移除

        _ = subviews.map {

            if $0.tag == itemIndex.rawValue + lxfFlag {

                $0.removeFromSuperview()

            }

        }

    }

}

调用:

RootTabBar()?.tabBar.showBadgOn()
func RootTabBar() -> MainTabBarVC? {

    return AppDelegate.sharedInstance.window?.rootViewController as? MainTabBarVC

}

常规的BaseTabBarVC的封装

import Foundation

import Logger

import UIKit

enum TabType: Int {

    case message

    case home

    case user

}

class MainTabBarVC: UITabBarController {

    override func viewDidLoad() {

        super.viewDidLoad()

        addChildViewControllers()

        delegate = self

        // 加阴影

        tabBar.layer.shadowColor = UIColor.black.withAlphaComponent(0.1).cgColor

        tabBar.layer.shadowOffset = CGSize(width: 0, height: 0)

        tabBar.layer.shadowOpacity = 1

        tabBar.layer.shadowRadius = 4

        tabBar.backgroundColor = UIColor.white


        // tabbar隐藏默认的黑线

        UITabBar.appearance().shadowImage = UIImage()

        UITabBar.appearance().backgroundImage = UIImage()

        UITabBar.appearance().isTranslucent = false


        // 设置tabBarItem选中与未选中的文字颜色

        UITabBarItem.appearance().setTitleTextAttributes([NSAttributedString.Key.foregroundColor: R.color.c939494()!], for: .normal)

        UITabBarItem.appearance().setTitleTextAttributes([NSAttributedString.Key.foregroundColor: R.color.c0a59f7()!], for: .selected)

        self.selectedIndex = 1

    }


    private func addChildViewControllers() {

        setChildViewController(MessageListVC(), title: "消息", imageName: "icon_tabbar_message")

        setChildViewController(XingtuVC(), title: "星途", imageName: "icon_tabbar_xintu")

        setChildViewController(ProfileVC(), title: "我的", imageName: "icon_tabbar_profile")

    }



    func setChildViewController(_ childController: UIViewController, title: String, imageName: String) {

        childController.tabBarItem = UITabBarItem(title: title, image: UIImage(named: imageName + "_nor")?.withRenderingMode(.alwaysOriginal), selectedImage: UIImage(named: imageName + "_sel")?.withRenderingMode(.alwaysOriginal))

        childController.title = title

        addChild(BaseNavigationController(rootViewController: childController))

    }

}


extension MainTabBarVC {

    func select(type: TabType, complete: (() -> Void)? = nil) {

        if #available(iOS 13.0, *) {

            if let navigation = self.selectedViewController as? UINavigationController {

                navigation.popToRootViewController(animated: false)

            }

        } else {

            if let navigation = selectedViewController as? UINavigationController {

                navigation.popToRootViewController(animated: true)

            }

        }


        DispatchQueue.main.asyncAfter(deadline: .now() + 0.01) {

            self.selectedIndex = type.rawValue

            complete?()

        }

    }

}



extension MainTabBarVC: UITabBarControllerDelegate {

    func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {

        let index = tabBarController.selectedIndex

        Logger.info(index)

    }


    func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {

        // 防止tab多次重复点击

        let vc = tabBarController.selectedViewController

        if vc == viewController {

            return false

        }

        return true

    }

}