mmick66 / CalendarView

An Easy to Use Calendar for iOS (Swift 5.0)
MIT License
595 stars 114 forks source link

KDCalendar not compatible with SwiftUI #108

Open shahedhossain opened 4 years ago

shahedhossain commented 4 years ago

MyCalendar structure for getting support in SwiftUI as following:

import SwiftUI
import KDCalendar

public struct MyCalendar: UIViewRepresentable {

    public func makeUIView(context: Context) -> CalendarView {
        return CalendarView()
    }

    public func updateUIView(_ calendar: CalendarView, context: Context) {
        let date: Date = Date()
        calendar.selectDate(date)
    }
}

It's shows only the week name and other parts of this calendar omitted from view and following errors occurred:

[Assert] negative or zero item sizes are not supported in the flow layout

Please find the details in StackOverflow

mmick66 commented 4 years ago

I have not built it for SwiftUI, that is true. If however you would like to add this support and do a Pull Request I would be forever grateful ;-)

suvov commented 4 years ago

@shahedhossain It works with SwiftUI. The common problem is having keyboard on screen, when presenting UIViewRepresented view (or any other actually). Zero or negative size assertion seems not to cause problems.

shahedhossain commented 4 years ago

Hello @suvov. Good day! Actually we are stacked on it. It would be best if you share your code snippet to the community to resolve such issues.

suvov commented 4 years ago

Hello @shahedhossain ! What I do is resign first responders (if any) before presenting the calendar. It might look like this in code:

// Resign first responder, using UIKit ( UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)

// Present view with calendar self.isCalendarPresented = true

Might look like this in context:

Button(action: {
   UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder),  to: nil, from: nil, for: nil)
   self.isCalendarPresented = true
}, label: {
   Image(systemName: "calendar")
}).sheet(isPresented: $isCalendarPresented, content: {
   self.calendarView()
})

Hope this helps, have a good day!