Home / IOS Development / ios – Swift CVCalendar does not update monthLabel

ios – Swift CVCalendar does not update monthLabel



I recently started using CVCalendar to create a CalendarView with a week̵

7;s presentation.

I created one UILabel cold monthLabel which has an outlet for the display controls

enter photo description here

The ViewController for this particular view is this:

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 expiration reference:

enter photo description here

The problem here is that even though I ask the UIL mark to change the text when the presented date is updated:

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

It does not update the value, but I get date.globalDescription value printed in the console.

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

I tried to use 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 only pushes a new view with the updated month and rejects the view, and while it basically contains the same code I used:

self.monthLabel.text = updatedMonthLabel.text

The monthLabel will not be updated


Source link