1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
- //
- // Date+Picker.swift
- // LMS
- //
- // Created by Suraj Kumar Mandal on 03/10/23.
- //
- import Foundation
- import UIKit
- extension UITextField {
- func datePicker<T>(target: T,
- doneAction: Selector,
- cancelAction: Selector,
- datePickerMode: UIDatePicker.Mode = .date) {
- let screenWidth = UIScreen.main.bounds.width
-
- func buttonItem(withSystemItemStyle style: UIBarButtonItem.SystemItem) -> UIBarButtonItem {
- let buttonTarget = style == .flexibleSpace ? nil : target
- let action: Selector? = {
- switch style {
- case .cancel:
- return cancelAction
- case .done:
- return doneAction
- default:
- return nil
- }
- }()
-
- let barButtonItem = UIBarButtonItem(barButtonSystemItem: style,
- target: buttonTarget,
- action: action)
-
- return barButtonItem
- }
-
- let datePicker = UIDatePicker(frame: CGRect(x: 0,
- y: 0,
- width: screenWidth,
- height: 216))
- datePicker.datePickerMode = datePickerMode
-
- if #available(iOS 13.4, *) {
- datePicker.preferredDatePickerStyle = .wheels
- } else {
- // Fallback on earlier versions
- }
- datePicker.minimumDate = Date()
- self.inputView = datePicker
-
- let toolBar = UIToolbar(frame: CGRect(x: 0,
- y: 0,
- width: screenWidth,
- height: 44))
- toolBar.setItems([buttonItem(withSystemItemStyle: .cancel),
- buttonItem(withSystemItemStyle: .flexibleSpace),
- buttonItem(withSystemItemStyle: .done)],
- animated: true)
- self.inputAccessoryView = toolBar
- }
- }
|