我们如何在 Swift 的 FSCalendar 中更改当前月份所有过去日期的标题颜色

How we can change current Month all Past Dates title Color in FSCalendar in Swift

我想更改 Swift 中 fscalendar 中所有过去日期的标题颜色 不是特定日期 我添加了这段代码:- 委托方法:-

func calendar(calendar: FSCalendar!, appearance: FSCalendarAppearance!, titleDefaultColorForDate date: NSDate!) -> UIColor! {
    
    if date .compare(Date()) == .orderedAscending {
        return .green
    }else {
        return .red
    }
}

尝试连接日历视图的委托和数据源并检查,因为我用这个方法得到了输出

class YourViewController: UIViewController, FSCalendarDelegate, FSCalendarDataSource, FSCalendarDelegateAppearance {
            
             .....
            
     override func viewDidLoad()
     {
          super.viewDidLoad()
          yourCalendarView.delegate = self
          yourCalendarView.dataSource = self
        
          //If you are using storyboard make sure data source and delegates connected
                   
     }
        
               ......
        
     func calendar(_ calendar: FSCalendar, appearance: FSCalendarAppearance, titleDefaultColorFor date: Date) -> UIColor? {
        
         //Remove timeStamp from date
         if date.removeTimeStamp!.compare(Date().removeTimeStamp!) == .orderedAscending {
        
            return .green
        
         }else if date.removeTimeStamp!.compare(Date().removeTimeStamp!) == .orderedDescending{
        
            return .red
        
         } else {
        
            return .black

         }
                
      }
}

extension Date {

    public var removeTimeStamp : Date? {
        guard let date = Calendar.current.date(from: Calendar.current.dateComponents([.year, .month, .day], from: self)) else {
         return nil
        }
        return date
    }
}

输出:

如果您希望过去和未来日期的标题颜色相同,请使用以下代码

func calendar(_ calendar: FSCalendar, appearance: FSCalendarAppearance, titleDefaultColorFor date: Date) -> UIColor? {
            
     if date.removeTimeStamp!.compare(Date().removeTimeStamp!) == .orderedAscending || date.removeTimeStamp!.compare(Date().removeTimeStamp!) == .orderedDescending {
                
         return .green
                
      } else {
                
         return .black
      }
            
}

输出: