Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FSCalendar - Change bg color of selected dates Swift 3?

Tags:

swift3

I am using FSCalendar to add calendar UI in my project. But I am unable to find any solution to change bg color of some selected dates. Can anyone suggest me how change color of selected dates in FSCalendar in swift 3?

import FSCalendar
  fileprivate weak var calendar: FSCalendar!
    override func viewDidLoad() {
    super.viewDidLoad()

    // In loadView or viewDidLoad
    let calendar = FSCalendar(frame: CGRect(x: 0, y: 0, width: 320, height: 300))
    calendar.dataSource = self
    calendar.delegate = self
    view.addSubview(calendar)
    self.calendar = calendar

    // Do any additional setup after loading the view.
}
like image 911
Saleem Khan Avatar asked Nov 28 '25 20:11

Saleem Khan


1 Answers

Please follow the steps.

1). Add delegate FSCalendarDelegateAppearance

2). Below is my reference array. You should have yours

let selectedDates = ["2017/01/08", "2017/01/06", "2017/01/17"]

3). Add your date formatter

fileprivate lazy var dateFormatter1: DateFormatter = {
    let formatter = DateFormatter()
    formatter.dateFormat = "yyyy/MM/dd"
    return formatter
}()

3). select the dates using

calendar.select(self.dateFormatter1.date(from: "2017/01/08"))

4). Add delegate method for color change.

func calendar(_ calendar: FSCalendar, appearance: FSCalendarAppearance, fillSelectionColorFor date: Date) -> UIColor? {

    let dateString = self.dateFormatter1.string(from: date)

    if self.selectedDates.contains(dateString) {
        return UIColor.green
    }

    return appearance.selectionColor
}

5). Now Run it. :)

like image 199
Nikunj Damani Avatar answered Dec 02 '25 05:12

Nikunj Damani



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!