どうも、ちょげ(@chogetarou)です。
DatePickerで選択できる日付の最大値を設定する方法を紹介します。
方法

DatePickerの日付の最大値を設定するには、引数「in」を使います。
具体的には、DatePickerの引数「in」に「…」を指定し、「…」の後に日付の最大値を指定します。
//引数「in」に「...日付の最大値」を指定
DatePicker("Selct Date", selection: $selectDate, in: ...maximumDate)
引数「in」を使うことで、DatePickerの日付の最大値を設定することが出来ます。
使用例
struct ContentView: View {
@State var selectDate = Date()
//日付の最大値は3年後に
let maximumDate = Calendar.current.date(byAdding: .year, value: 3, to: Date())!
var body: some View {
VStack {
DatePicker("Selct Date", selection: $selectDate, in: ...maximumDate)
}
}
}
コメント