本文介绍了如何在FSCalendar swift 3中更改事件数组的背景颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个数组的日期,想要为特定事件设置不同的颜色,请帮助执行此操作.

I'm having an 2arrays of dates and want to set different colors for particular event please help how to do this.

我正在尝试实现以下代码,但返回无效无颜色效果无错误,请耐心等待.

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
}

}
   }

推荐答案

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

Your dateFormat of DateFormatter and string date in array doesn't match so changes the dateFormat to yyyy-MM-dd and then check array contains object or not.

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

这篇关于如何在FSCalendar swift 3中更改事件数组的背景颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-14 10:11