使用 select 日期 swift 更新 FSCalendar Header 值

Update FSCalendar Header value with select date swift

我已将 FSCalendar 范围模式设置为周。之后 Header 我设置的日期格式 calendar.appearance.headerDateFormat = "dd MMMM yyyy EEEE" 它显示正确的格式。

已添加代码和图片。

func setupFSCalendar() {

        ViewCalendar.frame = CGRect(x: 0, y: 78, width: view.frame.width, height: 100)
        ViewCalendar.backgroundColor = ("#455a64").toColor()
        let calendar = FSCalendar(frame: CGRect(x: 0, y: 0, width: ViewCalendar.frame.width, height: 250))
        calendar.layoutIfNeeded()
        calendar.dataSource = self
        calendar.delegate = self
        calendar.appearance.weekdayTextColor = .white  // week name color
        calendar.appearance.headerDateFormat = "dd MMMM yyyy EEEE"
        calendar.appearance.headerTitleColor = UIColor.white
        calendar.appearance.headerTitleFont = UIFont.systemFont(ofSize: 12)
        calendar.appearance.titleWeekendColor = UIColor.white
        
        calendar.appearance.titleDefaultColor =  .white
        calendar.scrollDirection = .horizontal
        
        calendar.appearance.weekdayFont = UIFont.systemFont(ofSize: 12)
        calendar.appearance.titleFont = UIFont.systemFont(ofSize: 18)
        calendar.appearance.titleTodayColor = ("#54a767").toColor()
        calendar.appearance.selectionColor  = ("#788f9b").toColor()
        calendar.appearance.todayColor = nil
        
        calendar.appearance.subtitleWeekendColor = UIColor.white
        calendar.placeholderType = .none   /// to remove last month dates
        calendar.appearance.headerMinimumDissolvedAlpha = 0   // removes next prev month title.
        calendar.backgroundColor = .clear 
        
        calendar.firstWeekday = 2
        calendar.scope = .week
        
        ViewCalendar.addSubview(calendar)
    }


    func calendar(_ calendar: FSCalendar, didSelect date: Date, at monthPosition: FSCalendarMonthPosition) {
        print(date)
        calendar.setCurrentPage(date, animated: true)
        calendar.select(date)
        
    }

您可以通过以下方式替换选定日期可见 FSCalendarHeaderCell 的文本,如下所示

func calendar(_ calendar: FSCalendar, didSelect date: Date, at monthPosition: FSCalendarMonthPosition) {
    print(date)
    calendar.setCurrentPage(date, animated: true)
    calendar.select(date)
    
    //format the selectedDate and set to the header
    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = calendar.appearance.headerDateFormat
    
    for cell in calendar.calendarHeaderView.collectionView.visibleCells {
        (cell as! FSCalendarHeaderCell).titleLabel.text = dateFormatter.string(from: date)
    }
}