Skip to content

Instantly share code, notes, and snippets.

Created September 19, 2020 18:52
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
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()
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 =
@objc private func dismissTextField() {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment