我正在使用Swift的CVCalendar库,并且尝试自定义日历的外观。
我有一个名为“ FirstViewController”的ViewController,日历的UIView位于其中。在我的文件FirstViewController.swift中,我设法实现了属性自定义,其中CVCalendar提供了API函数。

但是我不知道自定义没有这些功能API的其他属性的正确方法是什么。例如,CVCalendar的类CVCalendarViewAppearanceDelegate具有许多属性,这些属性的默认值可以修改,例如dayLabelWeekdayInTextColor。

问题:如何正确修改FirstViewController.swift中的“ dayLabelWeekdayInTextColor”之类的属性?

注意,我创建了一个函数setupCalendarFormat(),用于修改属性。我尝试在viewDidLoad()和viewDidLayoutSubviews()中调用它,但是修改无法正常工作。

我的代码如下:

import UIKit
import CVCalendar

class FirstViewController: UIViewController {


    @IBOutlet weak var containerView: UIView!
    @IBOutlet weak var menuView: CVCalendarMenuView!
    @IBOutlet weak var calendarView: CVCalendarView!

 override func viewDidLoad() {
        super.viewDidLoad()
        //self.view.layoutIfNeeded()
        setupCalendarFormat()

    }

    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        //setupCalendarFormat()
        menuView.commitMenuViewUpdate()
        calendarView.commitCalendarViewUpdate()
    }


    func setupCalendarFormat(){

    //CircleView background color and alpha
    calendarView.appearance.dayLabelPresentWeekdayHighlightedBackgroundColor = UIColor.black //not working highlight color not changing to black

    //day numbers' color
    calendarView.appearance.dayLabelWeekdayInTextColor = UIColor.green // not working correctly, color is only changing to green after i select the number
    calendarView.appearance.dayLabelPresentWeekdayTextColor = UIColor.green // working , probably becuase by default the present day is selected

    }


}


extension FirstViewController: CVCalendarViewDelegate, CVCalendarMenuViewDelegate{


    //All these APIs are succesfully called
    func presentationMode() -> CalendarMode {
        return .weekView
    }

    func firstWeekday() -> Weekday {
        return .sunday
    }

    func dayOfWeekFont() -> UIFont {
        return UIFont.systemFont(ofSize: 12.0, weight: .medium)
    }
    func dayOfWeekTextColor() -> UIColor {
        return .black
    }

    func dayOfWeekTextUppercase() -> Bool {
        return false
    }

    func shouldShowWeekdaysOut() -> Bool {
        return true
    }


}

最佳答案

CVCalendar存储库为您提供一个CVCalendar Demo,它演示了如何设置日历元素的颜色。

它基本上是这样的:


将视图控制器设置为CVCalendar的委托
使委托符合委托协议
添加用于设置日历元素颜色的委托方法


当日历要显示例如文本元素时,它会通过调用特定的方法来询问其委托人(您的视图控制器)颜色,该方法将根据您的逻辑返回UIColor,例如:

extension ViewController: CVCalendarViewDelegate, CVCalendarMenuViewDelegate {

   func dayOfWeekTextColor(by weekday: Weekday) -> UIColor {
       return weekday == .sunday ? UIColor(red: 1.0, green: 0, blue: 0, alpha: 1.0) : UIColor.white
   }
}


请参见demo implementation

10-08 05:51