kvyatkovskys / KVKCalendar

A most fully customization calendar for Apple platforms 📅
https://kvyatkovskys.github.io/KVKCalendar/
MIT License
537 stars 118 forks source link

Header view reference will removed as soon as open the Calendar #349

Closed mohammadtakbiri closed 3 months ago

mohammadtakbiri commented 4 months ago

Describe the bug Hello, We think that there is a problem with this function: func dequeueCornerHeader(date: Date, frame: CGRect, type: CalendarType) -> UIView? As soon as we present/push the calendar view controller, this function will be called and will create our custom header view as we want it. But, right after the creation of this custom header view, I guess the reference to this view will be broken somehow! As we don't have access to this view anymore and we can not update the content of the header view. For example: I want to update the day and the week label on my custom header view as soon as user swipe the calendar to right or left. But it will not happen. I use this function to update my labels: func didSelectDates(_ dates: [Date], type: CalendarType, frame: CGRect?) But I guess it could be useful that I mention sometimes it work without any problem and will update my labels but in 90% of times, It's not working.

Screenshots Simulator Screen Recording - iPhone 14 Pro - 2024-05-28 at 09 40 37

My code:

   func dequeueCornerHeader(date: Date, frame: CGRect, type: CalendarType) -> UIView? {
        let headerBackView = UIView()
        headerBackView.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: 65)
        headerBackView.backgroundColor = .hexStringToColor(hex: "FCFCFC")

        dateLabel = UILabel()
        dateLabel.translatesAutoresizingMaskIntoConstraints = false
        dateLabel.font = .rubikFont(.medium, ofSize: 16)
        dateLabel.textAlignment = .center
        dateLabel.textColor = .white
        dateLabel.backgroundColor = .appColor(.AccentColor)
        dateLabel.text = date.exportToString(.day)
        dateLabel.layer.cornerRadius = 17.5
        dateLabel.layer.masksToBounds = true

        dateWeekLabel = UILabel()
        dateWeekLabel.translatesAutoresizingMaskIntoConstraints = false
        dateWeekLabel.font = .rubikFont(.regular, ofSize: 10)
        dateWeekLabel.textAlignment = .center
        dateWeekLabel.text = date.exportToString(.shortWeekDay)
        dateWeekLabel.textColor = .appColor(.AccentColor)

        let stackView = UIStackView()
        stackView.axis = .vertical
        stackView.spacing = 2
        stackView.alignment = .center
        stackView.translatesAutoresizingMaskIntoConstraints = false
        stackView.addArrangedSubview(dateWeekLabel)
        stackView.addArrangedSubview(dateLabel)
        headerBackView.addSubview(stackView)

        let lineView = UIView()
        lineView.translatesAutoresizingMaskIntoConstraints = false
        lineView.backgroundColor = .hexStringToColor(hex: "DEE1E5")
        headerBackView.addSubview(lineView)

        NSLayoutConstraint.activate([
            dateLabel.widthAnchor.constraint(equalToConstant: 35),
            dateLabel.heightAnchor.constraint(equalToConstant: 35),

            stackView.topAnchor.constraint(equalTo: headerBackView.topAnchor, constant: 8),
            stackView.bottomAnchor.constraint(equalTo: headerBackView.bottomAnchor, constant: -8),
            stackView.leadingAnchor.constraint(equalTo: headerBackView.leadingAnchor, constant: 10),

            lineView.topAnchor.constraint(equalTo: headerBackView.topAnchor),
            lineView.bottomAnchor.constraint(equalTo: headerBackView.bottomAnchor),
            lineView.widthAnchor.constraint(equalToConstant: 1),
            lineView.leadingAnchor.constraint(equalTo: headerBackView.leadingAnchor, constant: 55)
        ])

        return headerBackView
    }

func didSelectDates(_ dates: [Date], type: CalendarType, frame: CGRect?) {

        if let date = dates.first {
            print(date.exportToString(.day))
            DispatchQueue.main.async {
                self.dateLabel.text = date.exportToString(.day)
                print(date.exportToString(.day))
                self.dateWeekLabel.text = date.exportToString(.shortWeekDay)
                self.selectDate = date
                self.calendarView.reloadData()
                self.selectDayCalendarView.selectDate(date: date)

                self.viewModel.getCalendarEvents(
                    start: date.getTheDateWithHour(0, min: 0),
                    calendarTypes: self.calendarEventTypes,
                    periodInfoTypes: self.calendarPeriodTypes
                ) { result in
                    self.handleReceivedEventsFromAPI(result: result)
                }
            }
        }
    }

Thank you.

kvyatkovskys commented 4 months ago

@mohammadtakbiri Thank you for the issue. I'll take a look