CalendarView 使得 UIKit 的 UICalendarView 及其所有功能可以在 SwiftUI 中使用。
请注意,UICalendarView
使用 DateComponents 而不是 Date。CalendarView 为了保持一致性也使用相同的约定,但将来可能会增加对 Date
的支持。
import SwiftUI
import CalendarView
var body: some View {
CalendarView()
}
CalendarView 使用来自环境的 calendar, time zone 和 locale。
CalendarView()
.environment(\.locale, .init(identifier: "ja"))
可以使用 fontDesign
修饰符配置 字体设计。
CalendarView()
.fontDesign(.serif)
您还可以设置可用的日期范围。
CalendarView(availableDateRange: specialEvent)
您可以设置和更新日历中应可见的当前组件(年份,月份)。
VStack {
CalendarView(visibleDateComponents: $visibleComponents)
Button("Today") {
withAnimation {
visibleComponents = calendar.dateComponents([.year, .month], from: .now)
}
}
}
使用 decorating
修饰符来装饰特定的日期。
CalendarView()
.decorating([DateComponents(day: 16)])
装饰也可以自定义。
CalendarView()
.decorating(specialDates, systemImage: "star.fill", color: .yellow)
.decorating(otherDates, color: .green, size: .large)
CalendarView 支持选择单个和多个日期。
CalendarView(selection: $selectedDates)
您还可以配置哪些日期是可选的和可取消选择的。
CalendarView(selection: $selectedDates)
.selectable { dateComponents in
dateComponents.day > 15
}
.deselectable { dateComponents in
dateComponents.year == currentYear && dateComponents.month == currentMonth
}