Search code examples
iosswiftuilabelcvcalendar

Swift CVCalendar not updating monthLabel


I recently started using CVCalendar to create a CalendarView with a Week presentation.

I created a UILabel called monthLabel which has an outlet to the view's controller

enter image description here

The ViewController for this particular view is this one:

import UIKit
import CVCalendar

class EventsViewController: UIViewController {
    @IBOutlet weak var headerView: UIView!
    @IBOutlet weak var menuView: CVCalendarMenuView!
    @IBOutlet weak var calendarView: CVCalendarView!
    @IBOutlet weak var eventsTable: UITableView!
    @IBOutlet weak var monthLabel: UILabel!
    
    private var currentCalendar: Calendar?
    private var animationFinished: Bool = true
    
    let mainAppViewHeader: MainAppViewHeader = UIView.fromNib()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        loadViewHeader()
        setupTable()
    }
    
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()

        setupDatePicker()
    }
    
    override func awakeFromNib() {
        let timeZoneBias = 480 // (UTC+08:00)
        currentCalendar = Calendar(identifier: .gregorian)
        currentCalendar?.locale = Locale(identifier: "fr_FR")
        if let timeZone = TimeZone(secondsFromGMT: -timeZoneBias * 60) {
            currentCalendar?.timeZone = timeZone
        }
    }
    
    func setupDatePicker() {
        menuView.delegate = self
        calendarView.delegate = self
        
        menuView.backgroundColor = ColorManager.Gray5
        calendarView.backgroundColor = ColorManager.Gray5
        
        calendarView.appearance.dayLabelPresentWeekdaySelectedBackgroundColor = ColorManager.PrimaryColor
        calendarView.appearance.dayLabelWeekdaySelectedBackgroundColor = ColorManager.PrimaryColor
        
        if let currentCalendar = currentCalendar {
            monthLabel.text = CVDate(date: Date(), calendar: currentCalendar).globalDescription
        }
        
        menuView.commitMenuViewUpdate()
        calendarView.commitCalendarViewUpdate()
    }
    
    func loadViewHeader() {
        mainAppViewHeader.setupHeader()
        headerView.addSubview(mainAppViewHeader)
    }
    
    func setupTable() {
        let rowNib = UINib(nibName: "EventTableCell", bundle: nil)
        
        eventsTable.dataSource = self
        eventsTable.delegate = self
        
        eventsTable.register(rowNib, forCellReuseIdentifier: "eventCell")
        eventsTable.separatorStyle = .none
    }
}

extension EventsViewController: CVCalendarViewDelegate, CVCalendarMenuViewDelegate {
    func presentationMode() -> CalendarMode {
        return .weekView
    }
    
    func firstWeekday() -> Weekday {
        return .sunday
    }
    
    func shouldAutoSelectDayOnWeekChange() -> Bool {
        return true
    }
    
    func disableScrollingBeforeDate() -> Date {
        let currentDate = Date()
        return currentDate.addingTimeInterval(-8 * 24 * 60 * 60)
    }
    
    func disableScrollingBeyondDate() -> Date {
        let currentDate = Date()
        return currentDate.addingTimeInterval(180 * 24 * 60 * 60)
    }
    
    func maxSelectableRange() -> Int {
        return 1
    }
    
    func shouldSelectRange() -> Bool {
        false
    }
    
    func earliestSelectableDate() -> Date {
        let currentDate = Date()
        return currentDate.addingTimeInterval(-8 * 24 * 60 * 60)
    }
    
    func latestSelectableDate() -> Date {
        let currentDate = Date()
        return currentDate.addingTimeInterval(180 * 24 * 60 * 60)
    }
    
    func shouldShowWeekdaysOut() -> Bool {
        return true
    }
    
    func didShowPreviousWeekView(from startDayView: DayView, to endDayView: DayView) {
        print(startDayView, endDayView)
    }
    
    func presentedDateUpdated(_ date: CVDate) {
        monthLabel.text = "asdf" //calendarView.presentedDate.globalDescription
        print(date.globalDescription)
    }
}

extension EventsViewController: UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 3
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        guard let eventCell = tableView.dequeueReusableCell(withIdentifier: "eventCell") as? EventTableCell else {
            return UITableViewCell()
        }
        
        eventCell.setupCell()
        eventCell.updateConstraintsIfNeeded()
        
        return eventCell
    }
}

extension EventsViewController: UITableViewDelegate {
    
}

And the outlet reference:

enter image description here

The issue here is that even tho I'm telling the UILabel to change its text whenever the presented date is updated:

func presentedDateUpdated(_ date: CVDate) {
    monthLabel.text = "asdf" //calendarView.presentedDate.globalDescription
    print(date.globalDescription)
}

It's not updating its value, yet I get the date.globalDescription value printed in the console.

Is there something I'm missing when trying to update the label?

I tried using the code in the official example:

func presentedDateUpdated(_ date: CVDate) {
    if monthLabel.text != date.globalDescription && self.animationFinished {
        let updatedMonthLabel = UILabel()
        updatedMonthLabel.textColor = monthLabel.textColor
        updatedMonthLabel.font = monthLabel.font
        updatedMonthLabel.textAlignment = .center
        updatedMonthLabel.text = date.globalDescription
        updatedMonthLabel.sizeToFit()
        updatedMonthLabel.alpha = 0
        updatedMonthLabel.center = self.monthLabel.center
        
        let offset = CGFloat(48)
        updatedMonthLabel.transform = CGAffineTransform(translationX: 0, y: offset)
        updatedMonthLabel.transform = CGAffineTransform(scaleX: 1, y: 0.1)
        
        UIView.animate(withDuration: 0.35, delay: 0, options: UIView.AnimationOptions.curveEaseIn, animations: {
            self.animationFinished = false
            self.monthLabel.transform = CGAffineTransform(translationX: 0, y: -offset)
            self.monthLabel.transform = CGAffineTransform(scaleX: 1, y: 0.1)
            self.monthLabel.alpha = 0
            
            updatedMonthLabel.alpha = 1
            updatedMonthLabel.transform = CGAffineTransform.identity
            
        }) { _ in
            
            self.animationFinished = true
            self.monthLabel.frame = updatedMonthLabel.frame
            self.monthLabel.text = updatedMonthLabel.text
            self.monthLabel.transform = CGAffineTransform.identity
            self.monthLabel.alpha = 1
            updatedMonthLabel.removeFromSuperview()
        }
        
        self.view.insertSubview(updatedMonthLabel, aboveSubview: self.monthLabel)
    }
}

But this approach is only pushing a new view with the updated month and then dismissing the view, and while it contains basically the same code that I was using:

self.monthLabel.text = updatedMonthLabel.text

The monthLabel is not being updated, this is what happens when using the above code, it reverts back to original text.

enter image description here

If I remove this line:

updatedMonthLabel.removeFromSuperview()

Both texts get overlapped, so it's ignoring this line:

self.monthLabel.text = updatedMonthLabel.text

enter image description here


I've done some extra testing, and checked that the label is correctly linked with an outlet to my view, I changed the text color:

func presentedDateUpdated(_ date: CVDate) {
        print(Thread.current)
        print("Wololo")
        monthLabel.textColor = UIColor.green
        monthLabel.text = "asdf"
//        calendarView.contentController.refreshPresentedMonth()
//        monthLabel.text = "asdf" //calendarView.presentedDate.globalDescription
//        print(date.globalDescription)
    }

It prints:

<NSThread: 0x600002b6c100>{number = 1, name = main}
Wololo

N number of times I change the day by clicking on the calendar, however the text remains unchanged:

enter image description here


Solution

  • For some reason the code inside:

    override func awakeFromNib() {
        let timeZoneBias = 480 // (UTC+08:00)
        currentCalendar = Calendar(identifier: .gregorian)
        currentCalendar?.locale = Locale(identifier: "fr_FR")
        if let timeZone = TimeZone(secondsFromGMT: -timeZoneBias * 60) {
            currentCalendar?.timeZone = timeZone
        }
    }
    

    was in conflict with not updating the label, if we remove the above code and instead do this:

    func setupDatePicker() {
        menuView.delegate = self
        calendarView.delegate = self
        
        menuView.commitMenuViewUpdate()
        calendarView.commitCalendarViewUpdate()
        
        monthLabel.text = calendarView.presentedDate.globalDescription //This line at the bottom of this method
    }
    

    Everything works once again.

    If someone knows the reason behind it, feel free to make a comment and I'll add it to the answer.