【发布时间】:2021-06-04 22:58:52
【问题描述】:
我一直在玩 FSCalendar,它帮助我构建了自己的自定义日历。
因为它是用 UIKit 编写的,所以我在将它集成到我的 SwiftUI 项目时遇到了一些问题,例如在日历的两侧添加一个 Next 和 Previous 按钮。
这是我目前所拥有的:
ContentView,我使用 HStack 将按钮添加到日历的两侧
struct ContentView: View {
let myCalendar = MyCalendar()
var body: some View {
HStack(spacing: 5) {
Button(action: {
myCalendar.previousTapped()
}) { Image("back-arrow") }
MyCalendar()
Button(action: {
myCalendar.nextTapped()
}) { Image("next-arrow") }
}
}}
而 MyCalendar 结构体,为了集成 FSCalendar 库,是一个 UIViewRepresentable。 这也是我添加两个函数(nextTapped 和 previousTapped)的地方,它们应该在点击按钮时更改显示的月份:
struct MyCalendar: UIViewRepresentable {
let calendar = FSCalendar(frame: CGRect(x: 0, y: 0, width: 320, height: 300))
func makeCoordinator() -> Coordinator {
Coordinator(self)
}
func makeUIView(context: Context) -> FSCalendar {
calendar.delegate = context.coordinator
calendar.dataSource = context.coordinator
return calendar
}
func updateUIView(_ uiView: FSCalendar, context: Context) {
}
func nextTapped() {
let nextMonth = Calendar.current.date(byAdding: .month, value: 1, to: calendar.currentPage)
calendar.setCurrentPage(nextMonth!, animated: true)
print(calendar.currentPage)
}
func previousTapped() {
let previousMonth = Calendar.current.date(byAdding: .month, value: -1, to: calendar.currentPage)
calendar.setCurrentPage(previousMonth!, animated: true)
print(calendar.currentPage)
}
class Coordinator: NSObject, FSCalendarDelegateAppearance, FSCalendarDataSource, FSCalendarDelegate {
var parent: MyCalendar
init(_ calendar: MyCalendar) {
self.parent = calendar
}
func minimumDate(for calendar: FSCalendar) -> Date {
return Date()
}
func maximumDate(for calendar: FSCalendar) -> Date {
return Date().addingTimeInterval((60 * 60 * 24) * 365)
}
}}
这是它在模拟器中的样子:
如您所见,每当点击下一个或上一个按钮时,我都设法在终端中打印 currentPage,但实际日历中的 currentPage 并没有改变。 我该如何解决这个问题?
【问题讨论】:
-
接近它,而不用像在 UIKit 中那样思考它是如何完成的,即忘记目标动作。在 ContentView 中设置一个状态,如
currentDate。按钮会改变这种状态。然后让日历在日期更改时响应 - 将日期属性作为绑定传递给 MyCalendar,其中协调器(不应具有对结构的引用,而是对 UIView)将在更改时通过更新 MyCalendar 结构 UIView 进行响应.
标签: swiftui fscalendar