如何更改 FSCalendar swift 3 中事件数组的背景颜色?

How to change background color of events array in FSCalendar swift 3?

我有 2 个日期数组,想为特定事件设置不同的颜色请帮助如何做到这一点。

我正在尝试实现以下代码,但它不工作它返回 nil 没有颜色效果没有错误没有任何问题请帮助。

import UIKit
import FSCalendar

class myCalendarViewController: UIViewController,FSCalendarDelegate,FSCalendarDataSource,FSCalendarDelegateAppearance {

    var presentdays = [String]()
    var absentdays = [String]()

@IBOutlet weak var calendar: FSCalendar!

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

override func viewDidLoad() {
      super.viewDidLoad()
}

func calendar(_ calendar: FSCalendar, appearance: FSCalendarAppearance,  titleDefaultColorFor date: Date) -> UIColor? {
         presentdays = ["2017-06-03",
                        "2017-06-06",
                        "2017-06-12",
                        "2017-06-25"]

         absentdays = ["2017-06-10",
                       "2017-06-18",
                       "2017-06-15",
                       "2017-06-16"]

let datestring2 : String = dateFormatter1.string(from:date)

if presentdays.contains(datestring2)
{
    return UIColor.green
}
else if absentdays.contains(datestring2)
{
    return UIColor.red
}
else{
    return nil
}

}
   }

您的 dateFormat of DateFormatter 和数组中的字符串日期不匹配,因此将 dateFormat 更改为 yyyy-MM-dd 然后检查数组是否包含对象。

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