Show a new View from Button press Swift UI

前端 未结 2 1438
不知归路
不知归路 2020-12-10 19:11

I would like to be able to show a new view when a button is pressed on one of my views.

From the tutorials I have looked at and other answered questions here it seem

2条回答
  •  忘掉有多难
    2020-12-10 19:12

    For simple example you can use something like below

    import SwiftUI
    
    struct ExampleFlag : View {
        @State var flag = true
        var body: some View {
            ZStack {
                if flag {
                    ExampleView().tapAction {
                        self.flag.toggle()
                    }
                } else {
                    OtherExampleView().tapAction {
                        self.flag.toggle()
                    }
                }
            }
        }
    }
    struct ExampleView: View {
         var body: some View {
            Text("some text")
        }
    }
    struct OtherExampleView: View {
        var body: some View {
            Text("other text")
        }
    }
    

    but if you want to present more view this way looks nasty

    You can use stack to control view state without NavigationView

    For Example:

        class NavigationStack: BindableObject {
            let didChange = PassthroughSubject()
    
            var list: [AuthState] = []
    
            public func push(state: AuthState) {
                list.append(state)
                didChange.send()
            }
            public func pop() {
                list.removeLast()
                didChange.send()
            }
        }
    
        enum AuthState {
            case mainScreenState
            case userNameScreen
            case logginScreen
            case emailScreen
            case passwordScreen
        }
        struct NavigationRoot : View {
            @EnvironmentObject var state: NavigationStack
            @State private var aligment = Alignment.leading
    
            fileprivate func CurrentView() -> some View {
                switch state.list.last {
                case .mainScreenState:
                    return AnyView(GalleryState())
                case .none:
                    return AnyView(LoginScreen().environmentObject(state))
                default:
                    return AnyView(AuthenticationView().environmentObject(state))
                }
            }
            var body: some View {
            GeometryReader { geometry in
                self.CurrentView()
                    .background(Image("background")
                        .animation(.fluidSpring())
                        .edgesIgnoringSafeArea(.all)
                        .frame(width: geometry.size.width, height: geometry.size.height,
                               alignment: self.aligment))
                        .edgesIgnoringSafeArea(.all)
                        .onAppear {
                            withAnimation() {
                                switch self.state.list.last {
                                case .none:
                                        self.aligment = Alignment.leading
                                case .passwordScreen:
                                        self.aligment = Alignment.trailing
                                default:
                                        self.aligment = Alignment.center
                                }
                            }
                        }
                    }
                .background(Color.black)
            }
    
    }
    
    struct ExampleOfAddingNewView: View {
        @EnvironmentObject var state: NavigationStack
        var body: some View {
                VStack {
                    Button(action:{ self.state.push(state: .emailScreen) }){
                    Text("Tap me")
                }
    
            }
        }
    }
    
    
        struct ExampleOfRemovingView: View {
            @EnvironmentObject var state: NavigationStack
            var body: some View {
                VStack {
                    Button(action:{ self.state.pop() }){
                        Text("Tap me")
                    }
                }
            }
        }
    

    In my opinion this bad way, but navigation in SwiftUI much worse

提交回复
热议问题