Swift CVCalendar not updating monthLabel

Published

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

Source: Ios Questions

Published
Categorised as cvcalendar, ios, swift, uilabel

Answers

presentedDateUpdated is likely being called on a background thread making it so your UI changes aren’t being displayed. you can confirm this by printing Thread.current inside presentedDateUpdated.

a simple solution would be this:

func presentedDateUpdated(_ date: CVDate) {
    DispatchQueue.main.async { [weak self] in
        self?.monthLabel.text = "asdf" //calendarView.presentedDate.globalDescription
        print(date.globalDescription)
    }
}

Anahi Krajcik

Leave a Reply

Your email address will not be published. Required fields are marked *

Still Have Questions?


Our dedicated development team is here for you!

We can help you find answers to your question for as low as 5$.

Contact Us
faq