macOS SwiftUI Navigation for a Single View

后端 未结 3 1238
梦如初夏
梦如初夏 2021-01-05 02:47

I\'m attempting to create a settings view for my macOS SwiftUI status bar app. My implementation so far has been using a NavigationView, and NavigationLin

3条回答
  •  南笙
    南笙 (楼主)
    2021-01-05 03:14

    Here is a simple demo of possible approach for custom navigation-like solution. Tested with Xcode 11.4 / macOS 10.15.4

    Note: background colors are used for better visibility.

    struct ContentView: View {
        @State private var show = false
        var body: some View {
            VStack{
                if !show {
                    RootView(show: $show)
                        .frame(maxWidth: .infinity, maxHeight: .infinity)
                        .background(Color.blue)
                        .transition(AnyTransition.move(edge: .leading)).animation(.default)
                }
                if show {
                    NextView(show: $show)
                        .frame(maxWidth: .infinity, maxHeight: .infinity)
                        .background(Color.green)
                        .transition(AnyTransition.move(edge: .trailing)).animation(.default)
                }
            }
        }
    }
    
    struct RootView: View {
        @Binding var show: Bool
        var body: some View {
            VStack{
                Button("Next") { self.show = true }
                Text("This is the first view")
            }
        }
    }
    
    struct NextView: View {
        @Binding var show: Bool
        var body: some View {
            VStack{
                Button("Back") { self.show = false }
                Text("This is the second view")
            }
        }
    }
    

提交回复
热议问题