0

我的应用程序上有一个DatePicker,用户无法选择过去的日期,因为我正在创建一个通知

如何minimumDate在 SwiftUI 中实现等价物?

我尝试使用“if date < , else show alert”在我的保存按钮上创建一个警报Date(),但它不起作用。

4

1 回答 1

0

您可以提供一个范围DatePicker

@State private var date = Date()
let dateRange: ClosedRange<Date> = {
    let calendar = Calendar.current
    let startComponents = DateComponents(year: 2021, month: 1, day: 1)
    let endComponents = DateComponents(year: 2021, month: 12, day: 31, hour: 23, minute: 59, second: 59)
    return calendar.date(from:startComponents)!
        ...
        calendar.date(from:endComponents)!
}()

var body: some View {
    DatePicker(
        "Start Date",
         selection: $date,
         in: dateRange,
         displayedComponents: [.date, .hourAndMinute]
    )
}

来源:https ://developer.apple.com/documentation/swiftui/datepicker

于 2021-01-27T00:27:17.783 回答