How to show 2 calenders and how to disable future dates for both too in iOS

403 Views Asked by At

I am doing calendar view for my swift project. I have to pass From Date and To Fate to server. So, As per our design I have to show 2 calendars in UI. So, I design 2 calendars in UI and I am using following library to achieve it.

https://github.com/shoheiyokoyama/Koyomi

But, I have to disable future dates and user should not select them. Also It should not navigates to future months too.

enter image description here

Also I am not getting how to give actions to next and previous buttons action, Since I could not found them in that library.

Also I have to change colour for current date.

Any suggestions to achieve these.

Here is code

Viewcontroller.swift
//
//  ViewController.swift
//  Koyomi
//
//  Created by shoheiyokoyama on 10/09/2016.
//  Copyright (c) 2016 shoheiyokoyama. All rights reserved.
//

import UIKit
import Koyomi

class ViewController: UIViewController {
    
    @IBOutlet fileprivate weak var startDateCalenderSelectedDateLabel: UILabel!
    @IBOutlet fileprivate weak var ednDateCalenderSelectedDateLabel: UILabel!

    @IBOutlet fileprivate weak var startDateCalender: Koyomi! {
        didSet {
            startDateCalender.circularViewDiameter = 0.2
            startDateCalender.calendarDelegate = self
            startDateCalender.inset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
            startDateCalender.weeks = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat")
            startDateCalender.style = .standard
            startDateCalender.dayPosition = .center
            startDateCalender.selectionMode = .single(style: .background)
            startDateCalender.selectedStyleColor = UIColor.systemBlue
            startDateCalender.isHiddenOtherMonth = true
            startDateCalender.select(date: Date())
            startDateCalender
                .setDayFont(size: 14)
                .setWeekFont(size: 10)
        }
    }
    
    @IBOutlet fileprivate weak var endDateCalender: Koyomi! {
        didSet {
            endDateCalender.circularViewDiameter = 0.2
            endDateCalender.calendarDelegate = self
            endDateCalender.inset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
            endDateCalender.weeks = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat")
            endDateCalender.style = .standard
            endDateCalender.dayPosition = .center
            endDateCalender.selectionMode = .single(style: .background)
            endDateCalender.selectedStyleColor = UIColor.systemBlue
            endDateCalender.isHiddenOtherMonth = true
            endDateCalender
                .setDayFont(size: 14)
                .setWeekFont(size: 10)
        }
    }
    
    fileprivate let invalidPeriodLength = 90
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        //        currentDateLabel.text = startDateCalender.currentDateString()
    }
    
    @IBAction func startDateNextButtonTap(_button: Any) {
        
    }
    
    @IBAction func startDatePreviousButtonTap(_button: Any) {
        
    }
    
    @IBAction func endDateNextButtonTap(_button: Any) {
        
    }
    
    @IBAction func endDatePreviousButtonTap(_button: Any) {
        
    }
    // MARK: - Utility -
    
    fileprivate func date(_ date: Date, later: Int) -> Date {
        
        var components = DateComponents()
        components.day = later
        return (Calendar.current as NSCalendar).date(byAdding: components, to: date, options: NSCalendar.Options(rawValue: 0)) ?? date
    }
}

// MARK: - Tap Action

extension ViewController {
    @IBAction func tappedControl(_ sender: UISegmentedControl) {
        let month: MonthType = {
            switch sender.selectedSegmentIndex {
            case 0:  return .previous
            case 1:  return .current
            default: return .next
            }
        }()
        startDateCalender.display(in: month)
    }
    
    // Change koyomi style
    
    
    // Utility
    func configureStyle(_ style: KoyomiStyle) {
        
        startDateCalender.style = style
        startDateCalender.reloadData()
    }
    
    
}

// MARK: - KoyomiDelegate -

extension ViewController: KoyomiDelegate {
    func koyomi(_ koyomi: Koyomi, didSelect date: Date?, forItemAt indexPath: IndexPath) {
        
        if koyomi == startDateCalender {
            print("startDateCalender Selected: \(date)")
            startDateCalenderSelectedDateLabel.text = "\(date!)"

        } else if koyomi == endDateCalender {
            ednDateCalenderSelectedDateLabel.text =  "\(date!)"
        }
    }
    
    func koyomi(_ koyomi: Koyomi, currentDateString dateString: String) {
        
        if koyomi == startDateCalender {
            startDateCalenderSelectedDateLabel.text = dateString
        } else if koyomi == endDateCalender {
            ednDateCalenderSelectedDateLabel.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
    }
}
1

There are 1 best solutions below

0
Realrider On

How can I make calendar view in Swift

https://cocoapods.org/pods/JTAppleCalendar Try using this pod.

Looks like Pod Koyomi is old and not able to build using Xcode 12.