Skip to content

swiftuiux/d3-custom-navigation-swiftui

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Custom navigation swiftui

Experimenting with navigation link. if you find this idea interesting you can take and expend it into a more powerful solution. Live youtube

The result navigation

    @State var route : Router = .empty

    var body: some View {
        NavigationView{
            VStack{
                Button("go1") { route = .go1}
                Button("go2") { route = .go2}
                Button("go3") { route = .go3}
            }.navigation(route: $route)
            
        }.navigationViewStyle(.stack)
    }

1. Define sub view

struct SubView: View {

    let text: String

    var body: some View {
        Text("\(text)")
    }
}

2. Define routes

enum Router {
    case go1
    case go2
    case go3
    case empty

    @ViewBuilder
    var builder: some View {
        switch(self) {
            case .go1: SubView(text: "go1")
            case .go2: SubView(text: "go2")
            case .go3: SubView(text: "go3")
        default: EmptyView()
        }
    }
}

3. Define view modifier

struct NavigationModifire : ViewModifier{
    
    @State var isActive :  Bool = true
    
    @Binding var route : Router
    
    func body(content: Content) -> some View {
        content
            .background{
                NavigationLink(destination : route.builder, isActive: $isActive ){
                    EmptyView()
                }.hidden()
            }
            .onChange(of: isActive){
                if $0 == false { route = .empty }
            }
    }
    
}

extension View{
    @ViewBuilder
    func navigation(route : Binding<Router>) -> some View{
        if route.wrappedValue != .empty{
            modifier(NavigationModifire(route: route))
        }else { self }
    }
}
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy