TabBar

SwiftUI 标准 TabView 组件不够灵活,要自定义它,您必须修改 UITabBar 的外观代理,或者从头开始实现您自己的组件。这个库旨在解决这个问题。

目录

要求

安装

TabBar 可通过 Swift Package Manager 获取

Swift Package Manager

用法

要开始使用 TabBar,您必须创建一个枚举来实现 Tabbable 协议

enum Item: Int, Tabbable {
    case first = 0
    case second
    case third
    
    var icon: String {
        switch self {
            case .first:  // Name of icon of first item.
            case .second: // Name of icon of second item.
            case .third:  // Name of icon of third item.
        }
    }
    
    var title: String {
        switch self {
            case .first:  // Title of first item.
            case .second: // Title of second item.
            case .third:  // Title of third item.
        }
    }
}

之后,您将能够创建 TabBar 实例

struct ContentView: View {
    @State private var selection: Item = .first
    @State private var visibility: TabBarVisibility = .visible

    var body: some View {
        TabBar(selection: $selection, visibility: $visibility) {
            Text("First")
                .tabItem(for: Item.first)
            
            Text("Second")
                .tabItem(for: Item.second)
            
            Text("Third")
                .tabItem(for: Item.third)
        }
        .tabBar(style: CustomTabBarStyle())
        .tabItem(style: CustomTabItemStyle())
    }
}

完成这些操作后,将创建一个具有默认样式的选项卡栏。

自定义

TabBar 组件是高度可定制的。这通过引入 TabBarStyleTabItemStyle 协议来实现。通过实现每个协议,您将能够构建自定义的选项卡栏。 注意TabBar 会自动将任何选项卡栏样式推送到底部。

创建自定义样式后,您可以使用 tabBar(style:)tabItem(style:) 函数将它们注入到选项卡栏中。 这是默认样式的展示,以及您可以自定义选项卡栏的一个示例

贡献

如果您在某些方面遇到困难,请随时打开 issue。 也欢迎提交 Pull Request。

许可

TabBar 受 MIT 许可证的条款和条件约束。

MIT License

Copyright (c) 2021 Tamerlan Satualdypov

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.