SwiftUI How to push to next screen when tapping on Button

前端 未结 5 884
囚心锁ツ
囚心锁ツ 2020-12-21 18:02

I can navigate to next screen by using NavigationButton (push) or present with PresentationButton (present) but i want to push when i tap on Buttton()

Button         


        
相关标签:
5条回答
  • 2020-12-21 18:34

    As you can see to display the new view, add the NavigationLink with isActive: $pushView using <.hidden()> to hide the navigation "arrow".

    Next add Text("See More") with tapGesture to make the text respond to taps. The variable pushView will change (false => true) when you click "See More" text.

    import SwiftUI
    
    struct ContentView: View {
    
        @State var pushView = false
    
        var body: some View {
            NavigationView {
    
                List {
                    HStack{
                        Text("test")
                        Spacer()
                        NavigationLink(destination: NewView(), isActive: $pushView) {
                            Text("")
                        }.hidden()
                            .navigationBarTitle(self.pushView ? "New view" : "default view")
                        Text("See More")
                            .padding(.trailing)
                            .foregroundColor(Color.blue)
                            .onTapGesture {
                                self.pushView.toggle()
                        }
                    }
                }
            }
        }
    }
    
    struct NewView: View {
    
        var body: some View {
            Text("New View")
        }
    }
    

    ContentView picture

    NewView picture

    0 讨论(0)
  • 2020-12-21 18:35

    You need to use navigationBarBackButtonHidden by setting it true in your destination View like this below :-

    struct ContentView: View {
        var body: some View {
            NavigationView {
                NavigationButton(destination: DetailView()) {
                    Text("Click Me")
                }.navigationBarTitle(Text("Header"))             }
        }
    }
    struct DetailView: View {
        var body: some View {
            Text("Detail View")
            .navigationBarBackButtonHidden(true) //This will hide your back arrow button
    
        }
    }
    
    0 讨论(0)
  • 2020-12-21 18:39

    You can do using NavigationLink

    Note: Please try in real device. in simulator sometimes not work properly.

    struct MasterView: View {
        @State var selection: Int? = nil
    
        var body: some View {
            NavigationView {
                VStack {
                    NavigationLink(destination: DetailsView(), tag: 1, selection: $selection) {
                        Button("Press me") {
                            self.selection = 1
                        }
                    }
                }
            }
        }
    }
    
    struct DetailsView: View {
        @Environment(\.presentationMode) var presentation
    
        var body: some View {
            Group {
                Button("Go Back") {
                    self.presentation.wrappedValue.dismiss()
                }
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-21 18:41

    To tap on button and navigate to next screen,You can use NavigationLink like below

    NavigationView{
           NavigationLink(destination: SecondView()) {
                 Text("Login")
                  .padding(.all, 5)
                  .frame(minWidth: 0, maxWidth: .infinity,maxHeight: 45, alignment: .center)
                  .foregroundColor(Color.white)
           }
     }
    
    0 讨论(0)
  • 2020-12-21 18:46

    You can use NavigationLink to implement this:

    struct DetailsView: View {
        var body: some View {
            VStack {
                Text("Hello world")
            }
        }
    }
    
    struct ContentView: View {
        @State var selection: Int? = nil
        var body: some View {
            NavigationView {
                VStack {
                    NavigationLink(destination: DetailsView(), tag: 1, selection: $selection) {
                        Button("Press me") {
                            self.selection = 1
                        }
                    }
                }
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题