Skip to content

Repository files navigation

LLTabBarController

Swift Platform License SPM

一个轻量级、高度可定制的 UITabBarController 替代方案。在保持 API 简洁易用的同时,解决了系统 TabBar 的诸多痛点。

✨ 特性

  • 自定义 TabBar 高度 — 自由设置 TabBar 高度,不再受系统默认值限制。
  • 自定义 TabBar 项 — 每个 Tab 项支持任意 UIView 作为自定义视图(如 Lottie 动画),只需实现 LLTabBarItemSelectable 协议即可。
  • 凸起中心按钮 — 通过 raisedOffset 属性轻松实现类似 Twitter / Instagram 的中间凸起按钮。
  • 角标与红点 — 支持在任意 Tab 项上显示文字角标("99+")或小红点。
  • 分割线显隐 — 一键控制 TabBar 顶部分割线的显示与隐藏。
  • 智能导航跟随 — TabBar 智能跟随 push/pop 导航,始终锚定在正确的 ViewController 上,不会被转场动画遮挡。
  • 选中拦截代理 — 通过 LLTabBarControllerDelegate 拦截并有条件地阻止 Tab 切换。
  • 安全区域适配 — 自动适配全面屏 iPhone 的 Home Indicator 区域。
  • 零依赖 — 库本身不依赖任何第三方框架。

📋 环境要求

  • iOS 13.0+
  • Swift 5.0+
  • Xcode 17.0+

📦 安装

Swift Package Manager

Package.swift 中添加:

dependencies: [
    .package(url: "https://github.com/leelaxs/LLTabBarController.git", from: "0.0.6")
]

或在 Xcode 中:File → Add Packages… 输入仓库地址即可。

🚀 快速开始

基础用法

import LLTabBarController

class MyTabBarController: LLTabBarController {
    init() {
        let home = LLTabItem(
            viewController: UINavigationController(rootViewController: HomeVC()),
            tabBarItem: LLTabBarItem(model: LLTabBarItemModel(
                title: "首页",
                image: UIImage(named: "tab_home_nor"),
                selectedImage: UIImage(named: "tab_home_sel")
            ))
        )

        let messages = LLTabItem(
            viewController: UINavigationController(rootViewController: MessagesVC()),
            tabBarItem: LLTabBarItem(model: LLTabBarItemModel(
                title: "消息",
                image: UIImage(named: "tab_msg_nor"),
                selectedImage: UIImage(named: "tab_msg_sel")
            ))
        )

        let profile = LLTabItem(
            viewController: UINavigationController(rootViewController: ProfileVC()),
            tabBarItem: LLTabBarItem(model: LLTabBarItemModel(
                title: "我的",
                image: UIImage(named: "tab_profile_nor"),
                selectedImage: UIImage(named: "tab_profile_sel")
            ))
        )

        super.init(items: [home, messages, profile])
        isShowLine = true  // 显示分割线
    }
}

凸起中心按钮

适合"发布"、"添加"等突出操作:

let post = LLTabItem(
    viewController: PostViewController(),
    tabBarItem: LLTabBarItem(
        model: LLTabBarItemModel(raisedOffset: 10),
        customView: RaiseTabItemView()  // 你的自定义视图
    )
)

角标与红点

// 显示角标
tabBarController.showBadge("99+", at: 1)

// 清除角标
tabBarController.showBadge(nil, at: 1)

// 显示红点
tabBarController.showDot(at: 2)

代理拦截

通过代理控制 Tab 切换:

class MyViewController: UIViewController, LLTabBarControllerDelegate {
    func setup() {
        tabBarController.llDelegate = self
    }

    func llTabBarController(_ tabBarController: LLTabBarController, shouldSelect index: Int) -> Bool {
        // 未登录时拦截"我的"页面
        if index == 2 && !isLoggedIn {
            showLoginAlert()
            return false
        }
        return true
    }
}

🎨 自定义 TabBar 项

实现 LLTabBarItemSelectable 协议即可创建完全自定义的 TabBar 项:

public protocol LLTabBarItemSelectable {
    func configure(with model: LLTabBarItemModel)
    func deal(selected: Bool)
    func showBadge(isDot: Bool, value: String?)
}

示例 — Lottie 动画 Tab 项:

class LottieTabItemView: UIView, LLTabBarItemSelectable {
    private let animationView = LottieAnimationView()

    func configure(with model: LLTabBarItemModel) {
        // 配置 Lottie 动画
    }

    func deal(selected: Bool) {
        // 选中时播放动画
        selected ? animationView.play() : animationView.stop()
    }

    func showBadge(isDot: Bool, value: String?) {
        // 处理自定义视图上的角标展示
    }
}

// 使用
let tabItem = LLTabBarItem(
    model: LLTabBarItemModel(title: "我的", titleSelectedColor: .blue),
    customView: LottieTabItemView(path: Bundle.main.path(forResource: "tabbar_me", ofType: "json"))
)

协议中所有方法都有默认实现,按需实现即可。

📖 API 参考

LLTabBarController

属性 / 方法 说明
init(items: [LLTabItem]) 指定初始化方法
tabBarHeight: CGFloat 自定义 TabBar 高度(0 为系统默认)
isShowLine: Bool 是否显示顶部分割线
llDelegate: LLTabBarControllerDelegate? 选中代理
showBadge(_:at:) 在指定位置显示文字角标
showDot(at:) 在指定位置显示红点
toggleTabBarItem(index:) 代码切换 Tab

LLTabBarItemModel

属性 类型 默认值 说明
title String? nil 标题文字
font UIFont .systemFont(ofSize: 10) 标题字体
titleColor UIColor .systemGray 未选中时标题颜色
titleSelectedColor UIColor .systemBlue 选中时标题颜色
image UIImage? nil 未选中时图标
selectedImage UIImage? nil 选中时图标
imageSize CGSize 24×24 图标尺寸
space CGFloat 2 图标与标题间距
offsetY CGFloat 0 图标垂直偏移(正数向下)
raisedOffset CGFloat 0 凸起偏移距离

LLTabBarControllerDelegate

func llTabBarController(_ tabBarController: LLTabBarController, shouldSelect index: Int) -> Bool

🔗 参考资料

📄 许可证

LLTabBarController 基于 MIT 许可证开源。详见 LICENSE 文件。

About

轻量、可定制的 iOS TabBar 组件,支持凸起按钮、角标与自定义视图

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages