I am trying to achieve Date Range selection using FSCalendar, and i am achieving that but the view i want that is not proper . so can anyone help for that? I want it rounded as below.
My Current Output:
Desired output:
ViewController:
func initCalendar() {
calendar.dataSource = self
calendar.delegate = self
calendar.allowsMultipleSelection = true
calendar.firstWeekday = 2
calendar.backgroundColor = .clear
calendar.appearance.eventSelectionColor = UIColor.white
calendar.appearance.eventOffset = CGPoint(x: 0, y: 0)
calendar.placeholderType = .none
calendar.scope = .month
calendar.appearance.headerTitleColor = Defined.reuColor
calendar.appearance.weekdayTextColor = Defined.reuColor
//calendar.appearance.
calendar.today = nil // Hide the today circle
calendar.register(DateCalendarCell.self, forCellReuseIdentifier: "cell")
calendar.clipsToBounds = false // Remove top/bottom line
calendar.swipeToChooseGesture.isEnabled = true // Swipe-To-Choose
self.calendar.accessibilityIdentifier = "calendar"
}
extension AddRequestViewController: FSCalendarDelegate, FSCalendarDelegateAppearance, FSCalendarDataSource {
func calendar(_ calendar: FSCalendar, cellFor date: Date, at position: FSCalendarMonthPosition) -> FSCalendarCell {
let cell = calendar.dequeueReusableCell(withIdentifier: "cell", for: date, at: position)
return cell
}
func calendar(_ calendar: FSCalendar, willDisplay cell: FSCalendarCell, for date: Date, at position: FSCalendarMonthPosition) {
self.configure(cell: cell, for: date, at: position)
}
func calendar(_ calendar: FSCalendar, titleFor date: Date) -> String? {
return nil
}
func calendar(_ calendar: FSCalendar, appearance: FSCalendarAppearance, titleDefaultColorFor date: Date) -> UIColor? {
return Defined.reuColor
}
// MARK: - FSCalendarDelegate
func calendar(_ calendar: FSCalendar, boundingRectWillChange bounds: CGRect, animated: Bool) {
self.calendar.frame.size.height = bounds.height
}
func calendar(_ calendar: FSCalendar, shouldSelect date: Date, at monthPosition: FSCalendarMonthPosition) -> Bool {
return monthPosition == .current
}
func calendar(_ calendar: FSCalendar, shouldDeselect date: Date, at monthPosition: FSCalendarMonthPosition) -> Bool {
return monthPosition == .current
}
func calendar(_ calendar: FSCalendar, didSelect date: Date, at monthPosition: FSCalendarMonthPosition) {
if calendar.selectedDates.count == 3 {
for date in calendar.selectedDates {
calendar.deselect(date)
}
calendar.select(date)
}
if calendar.selectedDates.count == 1 {
let date = calendar.selectedDates[0]
self.startDate = date.dateTimeRequest
self.startDateForView = date.fullDateString
self.lblDateRange.text = "\(startDateForView) > \(startDateForView)"
self.joinPickedDateAndTime()
}
if calendar.selectedDates.count == 2 {
var first = calendar.selectedDates[0]
var second = calendar.selectedDates[1]
if second <= first {
let temp = first
first = second
second = temp
}
self.startDate = first.dateTimeRequest
self.endDate = second.dateTimeRequest
self.startDateForView = first.fullDateString
self.endDateForView = second.fullDateString
self.lblDateRange.text = "\(self.startDateForView) > \(self.endDateForView)"
self.joinPickedDateAndTime()
}
self.configureVisibleCells()
}
func calendar(_ calendar: FSCalendar, didDeselect date: Date, at monthPosition: FSCalendarMonthPosition) {
self.configureVisibleCells()
}
func calendar(_ calendar: FSCalendar, appearance: FSCalendarAppearance, eventOffsetFor date: Date) -> CGPoint {
return CGPoint(x: 0, y: 0)
}
// MARK: - Private functions
private func configureVisibleCells() {
calendar.visibleCells().forEach { (cell) in
let date = calendar.date(for: cell)
let position = calendar.monthPosition(for: cell)
self.configure(cell: cell, for: date!, at: position)
}
}
private func configure(cell: FSCalendarCell, for date: Date, at position: FSCalendarMonthPosition) {
guard let diyCell = (cell as? DateCalendarCell) else {return}
diyCell.circleImageView.isHidden = true
if position == .current {
var selectionType = SelectionType.none
if calendar.selectedDates.count == 2 {
var first = calendar.selectedDates[0]
var second = calendar.selectedDates[1]
if second <= first {
let temp = first
first = second
second = temp
}
if date == first {
selectionType = .leftBorder
} else if date == second {
selectionType = .rightBorder
} else if date >= first && date <= second {
selectionType = .middle
}
} else {
if calendar.selectedDates.contains(date) {
if calendar.selectedDates.count == 1 {
selectionType = .single
} else {
selectionType = .none
}
} else {
selectionType = .none
}
}
diyCell.selectionLayer.isHidden = false
diyCell.selectionType = selectionType
} else {
diyCell.selectionLayer.isHidden = true
diyCell.titleLabel.textColor = Defined.reuColor
}
}
}
I am trying to achieve Date Range selection using FSCalendar, and i am achieving that but the view i want that is not proper . so can anyone help for that? I want it rounded as below. My Current Output:
Desired output: ViewController:
DIYCalendarCell.swift