Skip to content

A SwiftUI component for macOS that makes complex view hierarchies possible

License

Notifications You must be signed in to change notification settings

lbrndnr/StackNavigationView

Repository files navigation

StackNavigationView

Twitter: @lbrndnr License

⚠️ SwiftUI now supports NavigationStack, making this repo redundant for new projects. I recommend to only use this if you have to support macOS 11 or 12. ⚠️

About

As of SwiftUI v2, NavigationView only supports a simple sidebar selection. This makes it impossible to push new views onto the view hierarchy, as one could do e.g. with UINavigationController. This project is a workaround that builds upon NavigationView to support complex view hierarchies.

Demo

Usage

The interface of StackNavigationView is very similar to the one of NavigationView, just make sure not to use NavigationLink inside of StackNavigationView though, it will result in undefined behaviour. You'll find the full example here.

struct ContentView: View {

@State private var selection: Int? = 0

var body: some View {
    
    return StackNavigationView(selection: $selection) {
        List {
            SidebarNavigationLink("Apples", destination: rootView(title: "Apples"), tag: 0, selection: $selection)
            SidebarNavigationLink("Bananas", destination: rootView(title: "Bananas"), tag: 1, selection: $selection)
            SidebarNavigationLink("Clementines", destination: rootView(title: "Clementines"), tag: 2, selection: $selection)
        }
        Text("Empty Selection")
    }
    .frame(minWidth: 600, minHeight: 400)
}

@ViewBuilder private func rootView(title: String) -> some View {
    VStack {
        Text("This is the root view of \(title)")
            .font(.system(size: 50))
            .bold()
        Spacer()
            .frame(height: 40)
        StackNavigationLink("Next", destination: ChildView(sidebar: title, level: 1))
    }
    .padding(20)
    .navigationTitle(title)
}
}

Requirements

StackNavigationView is a SwiftUI component for macOS. macOS Big Sur is required.

Author

I'm Laurin Brandner, I'm on Twitter.

License

StackNavigationView is licensed under the MIT License.

About

A SwiftUI component for macOS that makes complex view hierarchies possible

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages