I have a UIViewRepresentable of a third-party library component FSCalendar
. However, I need this to conform to type UIView... Is there a way to do this? Any help is appreciated :)
struct CalendarViewRepresentable: UIViewRepresentable {
typealias UIViewType = FSCalendar
var calendar = FSCalendar()
@Binding var selectedDate: Date
var calendarHeight: NSLayoutConstraint?
func updateUIView(_ uiView: FSCalendar, context: Context) { }
func makeUIView(context: Context) -> FSCalendar {
calendar.delegate = context.coordinator
calendar.dataSource = context.coordinator
calendar.translatesAutoresizingMaskIntoConstraints = false
calendar.setContentHuggingPriority(.required, for: .vertical)
calendar.setContentHuggingPriority(.required, for: .horizontal)
NSLayoutConstraint.activate([
calendar.topAnchor.constraint(equalTo: context.coordinator.topAnchor)
])
return calendar
}
func makeCoordinator() -> Coordinator {
Coordinator(self)
}
class Coordinator: NSObject, FSCalendarDelegate, FSCalendarDataSource {
var parent: CalendarViewRepresentable
init(_ parent: CalendarViewRepresentable) {
self.parent = parent
}
func calendar(_ calendar: FSCalendar, didSelect date: Date, at monthPosition: FSCalendarMonthPosition) {
parent.selectedDate = date
}
func calendar(_ calendar: FSCalendar, boundingRectWillChange bounds: CGRect, animated: Bool) {
parent.calendarHeight?.constant = bounds.height
parent.calendar.layoutIfNeeded()
}
}
}
struct HomeView: View {
@State private var selectedDate: Date = Date()
var body: some View {
VStack {
CalendarViewRepresentable(selectedDate: self.$selectedDate)
}
}
}