WWDC22 SALE: Save 50% on all my Swift books and bundles! >>

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

   

Save 50% in my Black Friday sale.

SAVE 50% To celebrate WWDC22, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

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.