Selection color changes the events color in FSCale

2019-03-06 01:22发布

问题:

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. How can I solve this issue as given in image below. The event dots on the blue selection color must come either green or red.

For image you can see this link : https://github.com/WenchaoD/FSCalendar/issues/919

回答1:

I think you have not set the eventSelectionColor Property of FSCalendar.

You can set it programmatically by using below code.

calendar.appearance.eventSelectionColor = UIColor.green

or

You can set it from storyboard in Attributes inspector field.Set the event selection color property of FSCalendar as shown in below image.

Hope this will help you.



回答2:

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.