Skip to content

Instantly share code, notes, and snippets.

@Yerazhas
Created September 19, 2020 18:52
Show Gist options
  • Save Yerazhas/adddca2945537b8be5d48c943a117696 to your computer and use it in GitHub Desktop.
Save Yerazhas/adddca2945537b8be5d48c943a117696 to your computer and use it in GitHub Desktop.
import SwiftUI
import UIKit
final class DatePickerTextField: UITextField {
@Binding var date: Date?
private let datePicker = UIDatePicker()
init(date: Binding<Date?>, frame: CGRect) {
self._date = date
super.init(frame: frame)
inputView = datePicker
datePicker.addTarget(self, action: #selector(datePickerDidSelect(_:)), for: .valueChanged)
datePicker.datePickerMode = .date
let toolBar = UIToolbar()
toolBar.sizeToFit()
let flexibleSpace = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
let doneButton = UIBarButtonItem(title: "Закрыть", style: .plain, target: self, action: #selector(dismissTextField))
toolBar.setItems([flexibleSpace, doneButton], animated: false)
inputAccessoryView = toolBar
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
@objc private func datePickerDidSelect(_ sender: UIDatePicker) {
date = sender.date
}
@objc private func dismissTextField() {
resignFirstResponder()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment