Skip to content

An adaptive SwiftUI TabView that switches between TabView and NavigationSplitView.

License

Notifications You must be signed in to change notification settings

mpdifran/AdaptiveTabView

Repository files navigation

AdaptiveTabView

An adaptive SwiftUI container that switches between TabView and NavigationSplitView based on horiontal size class. This framework allows you to easily build iPhone and iPad apps that conform to Apple's Human Interface Guidelines.

iPhone iPad
Preview iPhone Preview iPad

Here's an example of how it can be used:

@main
struct MyApp: App {
    @State private var selectedTab = MyFirstTab.identifier
    @State private var columnVisibility: NavigationSplitViewVisibility = .doubleColumn

    var body: some Scene {
        WindowGroup {
            AdaptiveTabView(
                appName: "My App", 
                selectedTab: selectedTab
            ) {
                MyFirstTab()
                MySecondTab()
                MyThirdTab()
            } defaultContent: {
                MyDefaultContentView()
            } defaultDetail: {
                MyDefaultDetailView()
            } sidebarExtraContent: {
                Section {
                    ForEach(folders) { (folder) in
                        FolderSidebarCell(folder)
                    }
                }   
            }
            .selectedTabTransformer(transformer)
        }
    }
    
    let transformer = SelectedTabTransformer { (kind, tabIdentifier) in
        switch kind {
        case .tabView:
            let sharedTabViewIdentifiers = [
                MyFirstTab.identifier,
                MySecondTab.identifier,
                MyThirdTab.identifier
            ]
            if !sharedTabViewIdentifiers.contains(tabIdentifier) {
                return MyFirstTab.identifier
            }
        case .sidebarView:
            break
        }
        return tabIdentifier
    }
}
extension MyFirstTab {
    static let identifier = TabIdentifier("MyFirstTab")
}

struct MyFirstTab: View, TitleImageProviding {
    let title = "My First Tab"
    let systemImageName = "1.square"
    let id = MyFirstTab.identifier

    var body: some View {
        ...
    }
}

About

An adaptive SwiftUI TabView that switches between TabView and NavigationSplitView.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages