Skip to content

Latest commit

 

History

History
77 lines (67 loc) · 2.81 KB

README.md

File metadata and controls

77 lines (67 loc) · 2.81 KB

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 {
        ...
    }
}