UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

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 {
                    DatePicker("EndDate", selection: $hibernation.endDate, displayedComponents: .date)

                    hibernation.showEnd = true

                } .padding()

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?



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

  .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 Superwall

SPONSORED Superwall lets you build & test paywalls without shipping updates. Run experiments, offer sales, segment users, update locked features and more at the click of button. Best part? It's FREE for up to 250 conversions / mo and the Superwall team builds out 100% custom paywalls – free of charge.

Learn More

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

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

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.