GO FURTHER, FASTER: Try the Swift Career Accelerator 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!

2      

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

2      

@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

2      

Hacking with Swift is sponsored by Proxyman.

SPONSORED Debug 10x faster with Proxyman. Your ultimate tool to capture HTTPs requests/ responses, natively built for iPhone and macOS. You’d be surprised how much you can learn about any system by watching what it does over the network.

Try Now!

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.