Sathish Kumar
Sathish Kumar

Reputation: 87

Multiple date range selection with custom views in swift 3

i need to develop the calendar like this attached image.i search lot of libraries.but no one is similar to this custom selection view.please some one help me.thank you..

enter image description here

Upvotes: 4

Views: 8204

Answers (2)

Mojtaba Hosseini
Mojtaba Hosseini

Reputation: 119340

iOS 16 - SwiftUI

You can use the SwiftUI's native MultiDatePicker control with supporting noncontiguous date selection:

MultiDatePicker("Dates", selection: $activityDates)

Demo

Upvotes: 0

Ravi Dhorajiya
Ravi Dhorajiya

Reputation: 1531

I think you use this library : Koyomi

Select date in programmatically

You can select specific date.

let today = Date()
var components = DateComponents()
components.day = 7
let weekLaterDay = Calendar.current.date(byAdding: components, toDate: today)
koyomi.select(date: today, to: weekLaterDay)

// If want to select only one day. 
koyomi.select(date: today)

// If want to select multiple day.
let dates: [Date] = [date1, date2, date3]
koyomi.select(dates: dates)

You can also unselect available.

koyomi.unselect(today, to: weekLaterDay) 
// If want to unselect only one day.
koyomi.unselect(today)
// If want to unselect multiple day.
let dates: [Date] = [date1, date2, date3]
koyomi.unselect(dates: dates)

// unselect all date
koyomi.unselectAll()

// You can also call this delegate.    

extension ViewController: KoyomiDelegate {
    func koyomi(_ koyomi: Koyomi, didSelect date: Date?, forItemAt indexPath: IndexPath) {
        print("You Selected: \(date)")
    }

    func koyomi(_ koyomi: Koyomi, currentDateString dateString: String) {
        currentDateLabel.text = dateString
    }

    @objc(koyomi:shouldSelectDates:to:withPeriodLength:)
    func koyomi(_ koyomi: Koyomi, shouldSelectDates date: Date?, to toDate: Date?, withPeriodLength length: Int) -> Bool {
        if length > invalidPeriodLength {
            print("More than \(invalidPeriodLength) days are invalid period.")
            return false
        }
        return true
    }
}

it's work for me. you can try that library.

Upvotes: 6

Related Questions