Selection color changes the events color in FSCalendar

前端 未结 2 1035
南方客
南方客 2021-01-27 17:35

I have a project using FSCalendar in swift 4. I have events as green and red color. But while selecting the particular dates , the color of events changes to selection color. Ho

2条回答
  •  执念已碎
    2021-01-27 18:18

    For multiple event with different colors, you have to use FSCalendarDelegateAppearance method.

    Example::::

    //MARK: - set formatter of date

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

    //MARK: - assign array of event

    var arrayOfEvent1 : [String] = ["2018-08-14","2018-08-15"]
    
    var arrayOfEvent2 : [String] = ["2018-08-14","2018-09-16"]
    
    
    
    func calendar(_ calendar: FSCalendar, numberOfEventsFor date: Date) -> Int 
    {
    
         let strDate = self.formatter.string(from:date)
    
             if arrayOfEvent1.contains(strDate) && arrayOfEvent2.contains(strDate) 
             {
                 return 2
             }
             else if arrayOfEvent1.contains(strDate)
             {
                 return 1
             }
             else if arrayOfEvent2.contains(strDate)
             {
                 return 1
             }
    
         return 0
    }
    
    
    func calendar(_ calendar: FSCalendar, appearance: FSCalendarAppearance,eventDefaultColorsFor date: Date) -> [UIColor]?
    {
    
    
        let strDate = formatter.string(from: date)
    
        if arrayOfEvent1.contains(strDate) && arrayOfEvent2.contains(strDate)
        {
            return [UIColor.red ,UIColor.blue]
        }
        else if arrayOfEvent1.contains(strDate)
        {
            return [UIColor.red]
        }
        else if arrayOfEvent2.contains(strDate)
        {
            return [UIColor.blue]
        }
    
        return [UIColor.clear]
    
    }
    
    func calendar(_ calendar: FSCalendar, appearance: FSCalendarAppearance, eventSelectionColorsFor date: Date) -> [UIColor]? {
          let strDate = formatter.string(from: date)
    
            if arrayOfEvent1.contains(strDate) && arrayOfEvent2.contains(strDate)                   
            {
                return [UIColor.red ,UIColor.blue]
            }
            else if arrayOfEvent1.contains(strDate)
            {
                 return [UIColor.red]
            }
            else if arrayOfEvent2.contains(strDate)
            {
                 return [UIColor.blue]
            }
    
                return [UIColor.clear]
        }
    

    After execution of above delegate method we get two events on "2018-08-14".first event color is red and second event color is blue.

    Hope this will help you.

提交回复
热议问题