Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

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

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

I'm trying to implement following code but its not working its returning nil no color effects no error nothing m stucked on this please help.

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
}

}
   }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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
}()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...