简体   繁体   English

当用户点击已选择的日期时关闭 SwiftUI DatePicker

[英]Dismiss SwiftUI DatePicker when user taps on already selected date

I want the user to select a date in the past by presenting a SwiftUI DatePicker .我希望用户通过显示 SwiftUI DatePicker来 select 过去的日期。 The date binding is set to today's date.日期绑定设置为今天的日期。

Now, I want the user to be able to select today's date .现在,我希望用户能够 select today's date But when the user taps on the already selected date, the DatePicker is not dismissed.但是当用户点击已经选择的日期时,DatePicker 不会被关闭。 It only is dismissed when the user taps on a date that was not already selected.只有当用户点击尚未选择的日期时,它才会被取消。 So in my case I want to dismiss the DatePicker when the user taps on 13.所以在我的例子中,我想在用户点击 13 时关闭 DatePicker。

日期选择器

Is there a way to dismiss the DatePicker when the user taps on the preselected date?当用户点击预选日期时,有没有办法关闭 DatePicker?

Here is how I use the DatePicker:下面是我如何使用 DatePicker:

struct DatePickerView: View {
    @Binding var date: Date

    var body: some View {
        DatePicker(
            "Select Date",
            selection: $date,
            in: ...Date(),
            displayedComponents: [.date]
        )
        .datePickerStyle(.graphical)
        .background(Color.white)
        .cornerRadius(8)
        .padding(.horizontal, 40)
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM