SmoltyTabView is a Swift package that provides a custom view for creating header views with tabs, similar to the profile views in Twitter or Instagram. It allows you to easily create a header view with multiple tabs beneath it that work seamlessly together.
Add the package to your project using Swift Package Manager (SPM):
- In Xcode, go to File > Swift Packages > Add Package Dependency.
- Enter the package URL:
https://github.com/Almaz5200/SmoltyTabView.git
. - Select the appropriate version or branch, and click Next.
To use SmoltyTabView, follow these steps:
-
Import the package:
import SmoltyTabView
-
Use
SmoltyHeaderView
and create your custom header, tab selection, and tab views:SmoltyHeaderView(headerBuilder: { // Your custom header view }, tabBuilder: { // Your custom tab selection view }, tabs: [ // Your custom TopTabViewElement(s) ], currentSelection: $currentSelection)
-
Instead of using the usual
ScrollView
, useEmbeddableScrollView
to make it compatible with SmoltyTabView.
Here's a simple example of how to use SmoltyTabView:
import SwiftUI
import SmoltyTabView
struct ContentView: View {
@State private var currentTab: Tab = .first
enum Tab: String, Hashable {
case first, second, third
}
var body: some View {
SmoltyHeaderView(headerBuilder: headerView, tabBuilder: tabBar, tabs: tabViews, currentSelection: $currentTab)
}
private func headerView() -> some View {
Text("SmoltyTabView Example")
.font(.largeTitle)
}
private func tabBar() -> some View {
HStack {
ForEach([Tab.first, Tab.second, Tab.third], id: \.self) { tab in
Button(tab.rawValue.capitalized) {
currentTab = tab
}
.padding()
.foregroundColor(currentTab == tab ? .blue : .gray)
}
}
}
private var tabViews: [TopTabViewElement<Tab>] {
[
firstTabView(),
secondTabView(),
thirdTabView()
]
}
private func firstTabView() -> TopTabViewElement<Tab> {
EmbeddableScrollView {
Text("First Tab Content")
.padding()
}
.topTabView(tab: .first)
}
private func secondTabView() -> TopTabViewElement<Tab> {
EmbeddableScrollView {
Text("Second Tab Content")
.padding()
}
.topTabView(tab: .second)
}
private func thirdTabView() -> TopTabViewElement<Tab> {
EmbeddableScrollView {
Text("Third Tab Content")
.padding()
}
.topTabView(tab: .third)
}
}
You can customize the appearance of the header and tab selection views by modifying their respective view builders (headerBuilder
and tabBuilder
). Additionally, you can customize the background color of the status bar using the statusBarBackground
parameter.
SmoltyTabView is available under the MIT License. See the LICENSE file for more information.