Load API Data in UICalendarView

163 Views Asked by At

I have gone through the UICalendarView implementation and it's working fine. I'm getting a list of holiday data and want to populate the UICalendarView to show the user of respective dates for holidays. I want to discriminate the date visually through background color as well. How I can populate the API data in ClaendarView Delegate and load in the UI.

    let calendarView = UICalendarView()
    calendarView.translatesAutoresizingMaskIntoConstraints = false
    calendarView.calendar = .current
    calendarView.locale = .current
    calendarView.fontDesign = .rounded
    calendarView.delegate = self
    calendarView.layer.cornerRadius = 12
    calendarView.backgroundColor = .white
    view.addSubview(calendarView)

    let selection = UICalendarSelectionSingleDate(delegate: self)
    calendarView.selectionBehavior = selection

    NSLayoutConstraint.activate([
        calendarView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 10),
        calendarView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -10),
        calendarView.heightAnchor.constraint(equalToConstant: 400),
        calendarView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor)])



func dateSelection(_ selection: UICalendarSelectionSingleDate, didSelectDate dateComponents: DateComponents?) {
    let inputFormatter = DateFormatter()
    inputFormatter.dateFormat = "yyyy-MM-dd"
    let showDate = dateComponents?.date
    inputFormatter.dateFormat = "MM/dd/yyyy"
    let resultString = inputFormatter.string(from: showDate!)
    print(resultString)
}

func dateSelection(_ selection: UICalendarSelectionSingleDate, canSelectDate dateComponents: DateComponents?) -> Bool {
    print(dateComponents)

    return true
}

func calendarView(_ calendarView: UICalendarView, decorationFor dateComponents: DateComponents) -> UICalendarView.Decoration? {
    for items in listData {
        print(items.date)

        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "yyyy-MM-dd"
        let date = dateFormatter.date(from: items.date ?? "")
        print(date)
    }

    return nil
}
0

There are 0 best solutions below