BLACK FRIDAY 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 {
                    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 Guardsquare

SPONSORED AppSweep by Guardsquare helps developers automate the mobile app security testing process with fast, free scans. By using AppSweep’s actionable recommendations, developers can improve the security posture of their apps in accordance with security standards like OWASP.

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.