Wednesday, March 27, 2024
HomeiOS Developmentswift - Allow the person to select a date from Calender for...

swift – Allow the person to select a date from Calender for non consecutive date solely in ios?


I’m new to the calendar. I’ve an array of dates which is non-consecutive. I need to choose and spotlight it initially. And, the unhighlighted must be non-clickable, i.e. the person is just in a position to click on on these dates which might be within the array of dates listing.

After exploring that I discovered a means that we are able to solely disable for selecting date via begin, finish and date vary solely.

I made a view mannequin as

class DateSelectionVM: ObservableObject{
    
    @Revealed var activeDate = [
        DateComponents(year: 2024, month: 3, day: 12),
        DateComponents(year: 2024, month: 3, day: 15),
        DateComponents(year: 2024, month: 3, day: 18),
        DateComponents(year: 2024, month: 3, day: 20),
        DateComponents(year: 2024, month: 3, day: 21),
        DateComponents(year: 2024, month: 3, day: 27)
    ]
    
    @Revealed var selectedDates: Set<DateComponents> = []
    
    var bounds: Vary<Date> {
        let calendar = Calendar.present
        var dates: [Date] = []
        for part in activeDate{
            dates.append(calendar.date(from: part)!)
        }
        
        if let vary = dates.dateRange {
            print("Date vary: (vary.lowerBound) to (vary.upperBound)")
            
            return vary
        } else {
            print("Array is empty")
        }
        
        return dates.first!..<dates.final!
    }
    
    
    init() {
        calulateDate()
    }
    
    personal func calulateDate(){
        for date in activeDate{
            selectedDates.insert(date)
        }
    }
}

the place I outlined a activeDate which must be displayed on the Calender as chosen and clickable.

I outline my view as

struct DateSelectionView: View {
    
    @ObservedObject var dateSelectionVM = DateSelectionVM()
    
    @Surroundings(.calendar) var calendar
    
    var physique: some View {
        
        VStack {
            Spacer()
            
            VStack{
                VStack(alignment: .main){
                    HStack{
                        Textual content("Your Every day Streak")
                            .foregroundColor(.blue)
                            .font(.title2)
                        Spacer()
                        Picture(systemName: "flame")
                            .foregroundColor(.blue)
                        Textual content("120")
                            .font(.title)
                            .daring()
                    }
                    Divider()
                        .padding(EdgeInsets(prime: 5, main: 0, backside: 5, trailing: 0))
                    Textual content("Interact each day")
                        .font(.system(measurement: 13))
                        .padding(EdgeInsets(prime: 5, main: 0, backside: 5, trailing: 0))
                    
                    
                    MultiDatePicker("Choose Dates", choice: $dateSelectionVM.selectedDates, in: dateSelectionVM.bounds)
                        .body(peak: 300)
                }
                
                .padding()
            }
            .background(.grey.opacity(0.2))
            .cornerRadius(10)
            
            Spacer()
            
        }
        .padding()
        .background(.purple.opacity(0.2))
        .navigationTitle("Stack")
        
    }
}

The issue I confronted was that I wasn’t in a position to disable different dates than activeDate.
I do know I gave a variety of dates that’s consecutive so, it permitted the person to click on. Is there one other option to repair this downside?

enter image description here

I’ve connected the picture. However what I would like is that I need to disable 13,14,16,17,19,22,23,24,25,26 as nicely. Can anybody assist me to repair it?



Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments