How to perform an action after NavigationLink is tapped?

后端 未结 4 934
梦毁少年i
梦毁少年i 2020-12-10 14:29

I have a Plus button in my first view. Looks like a FAB button. I want to hide it after I tap some step wrapped in NavigationLink. So far I have something like this:

4条回答
  •  北荒
    北荒 (楼主)
    2020-12-10 14:48

    I have tried an alternative approach to solving my problem. Initially I didn't use "List" because I had a problem with part of my code. But it cause another problem: PlusButton not disappearing on next screen after tapping NavigationLink. This is why I wanted to use simultaneousGesture - after tapping a link some actions would be performed as well (here: PlusButton would be hidden). But it didn't work well.

    I have tried an alternative solution. Using List (and maybe I will solve another problem later.

    Here is my alternative code. simultaneousGesture is not needed at all. Chevrons are added automatically to the list. And PlusButton hides the same I wanted.

    import SwiftUI
    
    struct BookingView: View {
    
        @State private var show_modal: Bool = false
    
        var body: some View {
            NavigationView {
                ZStack {
                    List {
                        DateView()
                            .listRowInsets(EdgeInsets())
    
                        ForEach(0 ..< 12) {item in
                            NavigationLink(destination: BookingDetailsView()) {
                                HStack {
                                    Text("Booking list item")
                                    Spacer()
                                }
                                .padding()
                            }
                        }
                    }.navigationBarTitle(Text("Booking"))
    
                    VStack {
                        Spacer()
                        Button(action: {
                            print("Button Pushed")
                            self.show_modal = true
                        }) {
                            Image(systemName: "plus")
                                .font(.largeTitle)
                                .frame(width: 60, height: 60)
                                .foregroundColor(Color.white)
                        }.sheet(isPresented: self.$show_modal) {
                            BookingAddView()
                        }
                        .background(Color.blue)
                        .cornerRadius(30)
                        .padding()
                        .shadow(color: Color.black.opacity(0.3), radius: 3, x: 3, y: 3)
                    }
                }
    
            }
        }
    
    }
    

提交回复
热议问题