Created
September 19, 2020 18:52
-
-
Save Yerazhas/adddca2945537b8be5d48c943a117696 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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