NEW: My new book Pro SwiftUI is out now – level up your SwiftUI skills today! >>

SOLVED: How to stop DatePicker closing a Navigation Link automatically?

Forums > SwiftUI

I have this navigation link here:

NavigationLink("Add End"){
       ChangeDate(hibernation: $item)
                                        }

(hibernation is an Array)

It leeds to this view with a form and a Date Picker:

struct ChangeDate: View{
    @Environment(\.presentationMode) var presentationMode
    @Binding var hibernation: Hibernation
    var body: some View {
      Vstack{
                Form{
                    DatePicker("EndDate", selection: $hibernation.endDate, displayedComponents: .date)

                }.cornerRadius(30)
                    .padding(60)
                Button("Submit"){
                    hibernation.showEnd = true
                    presentationMode.wrappedValue.dismiss()

                } .padding()
                    .font(.largeTitle)
                    .background(.blue)
                    .foregroundColor(.white)
                    .clipShape(Capsule())
            }
    }
}

My problem is that the Datepicker() when a date is selected dismisses the view. I would like the view to stay up so the user can finalise their selection and click the submit button. How do i do this?

Thankyou in advance!

   

I can't reproduce this behavior with what you've presented here. Mind posting some more code?

   

@roosterboy

There are these modifiers: I don't think that will effect it but..

 .padding(5)
  .background(.blue)
  .foregroundColor(.white)
  .clipShape(RoundedRectangle(cornerRadius: 5))

The navigation Link is in this forEach:

 ForEach($hibernations.items, id: \.self){ $item in
 }

I have another similar navigation Link that one works fine the only difference is that that one has other pickers.

However ever with another picker it still dismisses when a date is selected or the other picker is used

   

Hacking with Swift is sponsored by Play

SPONSORED Play is the first native iOS design tool created for designers and engineers. You can install Play for iOS and iPad today and sign up to check out the Beta of our macOS app with SwiftUI code export. We're also hiring engineers!

Click to learn more about Play!

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.