Tapping on Date Picker does not popup Picker view in iOS13 - ios

In the app when I tap on the UITextField that should give me a UIDatePicker does not show me UIDatePicker on updating the iPhone to iOS 13. Works fine with iOS 12+ versions.
I am working on latest Xcode 11 and macOS Mojave 10.14.6
This is the code I am using:
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
let dateString: String!
activeTextField = textField
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd-MM-yyyy"
if activeTextField == fromDateTextField && fromDateTextField.text != "" {
dateString = fromDateTextField.text
let date = dateFormatter.date(from: dateString!)
datePickerView.setDate(date!, animated: false)
} else if activeTextField == toDateTextField && activeTextField.text != "" {
dateString = toDateTextField.text
let date = dateFormatter.date(from: dateString!)
datePickerView.setDate(date!, animated: false)
}
UIView.animate(withDuration: 0.3) {
self.datePickerContainerView.frame = CGRect(x: 0,
y: UIScreen.main.bounds.size.height - 250,
width: UIScreen.main.bounds.size.width,
height: 250)
}
return false
}

This worked for me :
Initialise : let picker = UIDatePicker()
and call createToolbar function in viewDidLoad
func createToolbar() {
let toolbar = UIToolbar()
toolbar.sizeToFit()
let doneButton = UIBarButtonItem(title: "Set Date", style: .done, target: nil, action: #selector(showDate))
toolbar.setItems([doneButton], animated: false)
fromDateTextField.inputAccessoryView = toolbar
fromDateTextField.inputView = picker
toDateTextField.inputAccessoryView = toolbar
toDateTextField.inputView = picker
picker.datePickerMode = .date
}
#objc func showDate() {
let dateFormat = DateFormatter()
dateFormat.dateFormat = "dd-MM-yyyy"
let string = dateFormat.string(from: picker.date)
if fromDateTextField.isFirstResponder {
fromDateTextField.text = string
}
if toDateTextField.isFirstResponder {
toDateTextField.text = string
}
self.view.endEditing(true)
}
Thank you #rmmaddy for the inputs :)

class ViewController: UIViewController {
#IBOutlet weak var dateField: UITextField!
var datePicker :UIDatePicker!
override func viewDidLoad() {
super.viewDidLoad()
datePicker = UIDatePicker.init(frame: CGRect(x: 0, y: 0, width: view.bounds.size.width, height: 200))
datePicker.addTarget(self, action: #selector(self.dateChanged), for: .allEvents)
dateField.inputView = datePicker
let doneButton = UIBarButtonItem.init(title: "Done", style: .done, target: self, action: #selector(self.datePickerDone))
let toolBar = UIToolbar.init(frame: CGRect(x: 0, y: 0, width: view.bounds.size.width, height: 44))
toolBar.setItems([UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.flexibleSpace, target: nil, action: nil), doneButton], animated: true)
dateField.inputAccessoryView = toolBar
}
#objc func datePickerDone() {
dateField.resignFirstResponder()
}
#objc func dateChanged() {
dateField.text = "\(datePicker.date)"
}
}
try this

Related

How to add one DatePicker to three textfields in swift

I need to add DatePicker to three textfields for that
I have created one datepicker like below
func addInputViewDatePicker(target: Any, selector: Selector) {
let screenWidth = UIScreen.main.bounds.width
//Add DatePicker as inputView
let datePicker = UIDatePicker(frame: CGRect(x: 0, y: 0, width: screenWidth, height: 216))
datePicker.datePickerMode = .date
self.inputView = datePicker
//Add Tool Bar as input AccessoryView
let toolBar = UIToolbar(frame: CGRect(x: 0, y: 0, width: screenWidth, height: 44))
let flexibleSpace = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
let cancelBarButton = UIBarButtonItem(title: "Cancel", style: .plain, target: self, action: #selector(cancelPressed))
let doneBarButton = UIBarButtonItem(title: "Done", style: .plain, target: target, action: selector)
toolBar.setItems([cancelBarButton, flexibleSpace, doneBarButton], animated: false)
self.inputAccessoryView = toolBar
}
#objc func cancelPressed() {
self.resignFirstResponder()
}
and i am adding datepicker to three textfields in doneButtonPressed like below
#objc func doneButtonPressed() {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
if let datePicker = self.fromDateTextfield.inputView as? UIDatePicker {
self.fromDateTextfield.text = dateFormatter.string(from: datePicker.date)
}
if let datePicker = self.toDateTextfield.inputView as? UIDatePicker {
self.toDateTextfield.text = dateFormatter.string(from: datePicker.date)
}
if let datePicker = self.lastbidDateTextfield.inputView as? UIDatePicker {
self.lastbidDateTextfield.text = dateFormatter.string(from: datePicker.date)
}
self.lastbidDateTextfield.resignFirstResponder()
self.toDateTextfield.resignFirstResponder()
self.fromDateTextfield.resignFirstResponder()
}
and in viewdidload:
override func viewDidLoad() {
super.viewDidLoad()
fromDateTextfield.addInputViewDatePicker(target: self, selector: #selector(doneButtonPressed))
toDateTextfield.addInputViewDatePicker(target: self, selector: #selector(doneButtonPressed))
lastbidDateTextfield.addInputViewDatePicker(target: self, selector: #selector(doneButtonPressed))
}
with above code if i add date in fromDateTextfield then in all three textfields same date is adding.. without going to that remaining two textfields
i want to add different dates to three textfields.. how to do that with above code..
if my above code is wrong please help me with new correct working code for three three textfields
Set a tag to every UIBarButtonItem from 0 to 2
func addInputViewDatePicker(target: Any, selector: Selector,tag:Int)
.....
doneBarButton.tag = tag
}
#objc func doneButtonPressed(_ sender: UIBarButtonItem) {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
let arr = [fromDateTextfield,toDateTextfield,lastbidDateTextfield]
let current = arr[sender.tag]
if let datePicker = current.inputView as? UIDatePicker {
current.text = dateFormatter.string(from: datePicker.date)
current.resignFirstResponder()
}
}

Textfield and datepicker change values

I try to use this code to get date picker when clicking on a text field:
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var DOBTextField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
DOBTextField.addInputViewDatePicker(target: self, selector: #selector(doneButtonPressed))
}
#objc func doneButtonPressed() {
if let datePicker = self.DOBTextField.inputView as? UIDatePicker {
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = .none
dateFormatter.timeStyle = .short
dateFormatter.locale = Locale.init(identifier: "it_IT")
self.DOBTextField.text = dateFormatter.string(from: datePicker.date)
}
self.DOBTextField.resignFirstResponder()
}
}
extension UITextField {
func addInputViewDatePicker(target: Any, selector: Selector) {
let screenWidth = UIScreen.main.bounds.width
//Add DatePicker as inputView
let datePicker = UIDatePicker(frame: CGRect(x: 0, y: 0, width: screenWidth, height: 216))
datePicker.datePickerMode = .time
datePicker.locale = Locale.init(identifier: "it_IT")
self.inputView = datePicker
//Add Tool Bar as input AccessoryView
let toolBar = UIToolbar(frame: CGRect(x: 0, y: 0, width: screenWidth, height: 44))
let flexibleSpace = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
let cancelBarButton = UIBarButtonItem(title: "Cancel", style: .plain, target: self, action: #selector(cancelPressed))
let doneBarButton = UIBarButtonItem(title: "Done", style: .plain, target: target, action: selector)
toolBar.setItems([cancelBarButton, flexibleSpace, doneBarButton], animated: false)
self.inputAccessoryView = toolBar
}
#objc func cancelPressed() {
self.resignFirstResponder()
}
}
But when I click on text field again, time on date picker move forward on 3 dimensions. How this can be fixed? I want time on date picker to rest the same as it is on text field.
I try to implement this code, but it crash:
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var DOBTextField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
DOBTextField.addInputViewDatePicker(target: self, selector: #selector(doneButtonPressed), textFieldText: DOBTextField.text ?? "")
}
#objc func doneButtonPressed() {
if let datePicker = self.DOBTextField.inputView as? UIDatePicker {
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = .none
dateFormatter.timeStyle = .short
dateFormatter.locale = Locale.init(identifier: "it_IT")
self.DOBTextField.text = dateFormatter.string(from: datePicker.date)
}
self.DOBTextField.resignFirstResponder()
}
}
extension UITextField {
func addInputViewDatePicker(target: Any, selector: Selector , textFieldText: String?) {
let screenWidth = UIScreen.main.bounds.width
//Add DatePicker as inputView
let datePicker = UIDatePicker(frame: CGRect(x: 0, y: 0, width: screenWidth, height: 216))
datePicker.datePickerMode = .time
datePicker.locale = Locale.init(identifier: "it_IT")
self.inputView = datePicker
if textFieldText != nil {
/// Set Textfield date & time
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = .none
dateFormatter.timeStyle = .short
dateFormatter.locale = Locale.init(identifier: "it_IT")
datePicker.date = dateFormatter.date(from: textFieldText!)!
}
else {
/// set current date & time
datePicker.date = Date()
}
//Add Tool Bar as input AccessoryView
let toolBar = UIToolbar(frame: CGRect(x: 0, y: 0, width: screenWidth, height: 44))
let flexibleSpace = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
let cancelBarButton = UIBarButtonItem(title: "Cancel", style: .plain, target: self, action: #selector(cancelPressed))
let doneBarButton = UIBarButtonItem(title: "Done", style: .plain, target: target, action: selector)
toolBar.setItems([cancelBarButton, flexibleSpace, doneBarButton], animated: false)
self.inputAccessoryView = toolBar
}
#objc func cancelPressed() {
self.resignFirstResponder()
}
}
How this can be fixed?
As per I understand, You want to textfield's time when datepicker opens. You just have to set date for DatePicker when it will be initialized.
Set DatePicker's date :
extension UITextField {
func addInputViewDatePicker(target: Any, selector: Selector , textFieldText:String?) {
//Add DatePicker as inputView
//Set date for date Picker
if textFieldText != nil {
/// Set Textfield date & time
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = .none
dateFormatter.timeStyle = .short
dateFormatter.locale = Locale.init(identifier: "it_IT")
datePicker.date = dateFormatter.date(from: textFieldText!)!
}
else {
/// set current date & time
datePicker.date = Date()
}
//Add Tool Bar as input AccessoryView
}
}
Change in your viewDidLoad:
override func viewDidLoad() {
super.viewDidLoad()
DOBTextField.addInputViewDatePicker(target: self, selector: #selector(doneButtonPressed), textFieldText: DOBTextField.text ?? "")
}
I hope you understand what you have to set with datepicker. If this code is not working as you want then take Action of textfield (Editing Did Begin) & try to set textfield inputView in Action.
It seems this problem only appears on simulators. When I try to test this code on real device everything works fine. May be it's just Xcode bug.

Cannot convert string to date for datePicker

I try to get string from textfield, then convert it to date and put this value in datepicker. But for some reason it fails everytime and I get: "There is issue in dateformatter. Not able to convert string to Date".
I don't understand what I'm doing wrong and how I get this works.
My code:
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var DOBTextField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
DOBTextField.addInputViewDatePicker(target: self, selector: #selector(doneButtonPressed), textFieldText: DOBTextField.text)
}
#objc func doneButtonPressed() {
if let datePicker = self.DOBTextField.inputView as? UIDatePicker {
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = .long
dateFormatter.timeStyle = .none
self.DOBTextField.text = dateFormatter.string(from: datePicker.date)
}
self.DOBTextField.resignFirstResponder()
}
}
extension UITextField {
func addInputViewDatePicker(target: Any, selector: Selector , textFieldText: String?) {
let screenWidth = UIScreen.main.bounds.width
let datePicker = UIDatePicker(frame: CGRect(x: 0, y: 0, width: screenWidth, height: 216))
datePicker.datePickerMode = .date
self.inputView = datePicker
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = .long
dateFormatter.timeStyle = .none
if textFieldText != nil {
if let date = dateFormatter.date(from: textFieldText!) {
datePicker.date = date
} else {
print("There is issue in dateformatter. Not able to convert string to Date ")
}
}
else {
datePicker.date = Date()
}
//Add Tool Bar as input AccessoryView
let toolBar = UIToolbar(frame: CGRect(x: 0, y: 0, width: screenWidth, height: 44))
let flexibleSpace = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
let cancelBarButton = UIBarButtonItem(title: "Cancel", style: .plain, target: self, action: #selector(cancelPressed))
let doneBarButton = UIBarButtonItem(title: "Done", style: .plain, target: target, action: selector)
toolBar.setItems([cancelBarButton, flexibleSpace, doneBarButton], animated: false)
self.inputAccessoryView = toolBar
}
#objc func cancelPressed() {
self.resignFirstResponder()
}
}
I don't understand what I'm doing wrong and why this code doesn't work. May be someone can help me resolve this issue?
As I see when you call addInputViewDatePicker on viewDidLoad, the value of textFieldText is "" that's why you see your print from start
you can try use this code (but, I guess, you can optimize it):
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var DOBTextField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
prepareDOBTextField()
DOBTextField.addInputViewDatePicker(target: self, selector: #selector(doneButtonPressed), textFieldText: DOBTextField.text)
}
#objc func doneButtonPressed() {
if let datePicker = self.DOBTextField.inputView as? UIDatePicker {
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = .long
dateFormatter.timeStyle = .none
self.DOBTextField.text = dateFormatter.string(from: datePicker.date)
}
self.DOBTextField.resignFirstResponder()
}
func prepareDOBTextField() {
let date = Date()
let formatter = DateFormatter()
formatter.dateStyle = .long
formatter.timeStyle = .none
let result = formatter.string(from: date)
self.DOBTextField.text = result
}
}
extension UITextField {
func addInputViewDatePicker(target: Any, selector: Selector , textFieldText: String?) {
let screenWidth = UIScreen.main.bounds.width
let datePicker = UIDatePicker(frame: CGRect(x: 0, y: 0, width: screenWidth, height: 216))
datePicker.datePickerMode = .date
self.inputView = datePicker
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = .long
dateFormatter.timeStyle = .none
if textFieldText != nil && textFieldText != "" {
if let date = dateFormatter.date(from: textFieldText!) {
datePicker.date = date
} else {
print("There is issue in dateformatter. Not able to convert string to Date ")
}
}
else {
datePicker.date = Date()
}
//Add Tool Bar as input AccessoryView
let toolBar = UIToolbar(frame: CGRect(x: 0, y: 0, width: screenWidth, height: 44))
let flexibleSpace = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
let cancelBarButton = UIBarButtonItem(title: "Cancel", style: .plain, target: self, action: #selector(cancelPressed))
let doneBarButton = UIBarButtonItem(title: "Done", style: .plain, target: target, action: selector)
toolBar.setItems([cancelBarButton, flexibleSpace, doneBarButton], animated: false)
self.inputAccessoryView = toolBar
}
#objc func cancelPressed() {
self.resignFirstResponder()
}
}
You can do this by follow my code below.
#objc func doneButtonPressed() {
//Converting string to date
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm"
let date = dateFormatter.date(from: DOBTextField.text!)
//Sets the date in the date picker
datepicker.setDate(date!, animated: true)
}
So first you're converting your textfield (DOBTextField.text) into Date.
Right after you're setting the datePicker to be the correct date.
In this case you will have to enter exactly like this in the textfield:
"2019-12-18 14:50"
Solved this question. I get rid of extenstion and write code for datepicker in ViewController.
import UIKit
class ViewController: UIViewController {
#IBOutlet var textField: UITextField!
let datePicker = UIDatePicker()
override func viewDidLoad() {
super.viewDidLoad()
createDatePicker()
}
func createDatePicker() {
textField.inputView = datePicker
datePicker.datePickerMode = .date
datePicker.maximumDate = Date()
let toolbar = UIToolbar()
toolbar.sizeToFit()
let doneButton = UIBarButtonItem(barButtonSystemItem: .done, target: nil, action: #selector(doneClicked))
let flexibleSpace = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
toolbar.setItems([flexibleSpace, doneButton], animated: true)
textField.inputAccessoryView = toolbar
}
#objc func doneClicked() {
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = .long
dateFormatter.timeStyle = .none
textField.text = dateFormatter.string(from: datePicker.date)
self.view.endEditing(true)
}
}
After I added to viewWillAppear actual values of datepicker:
override func viewWillAppear(_ animated: Bool) {
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = .long
dateFormatter.timeStyle = .none
datePicker.date = dateFormatter.date(from: textfield.text!)!
}

I want in one textfield it can be show keyboard and datepicker

I want show keyboard when user click the button on the right side and show the datePicker when user click the textField,
#IBAction func doEditBirthDate(_ sender: UITextField) {
if (txfBirthDate.isEditing){
btnPhotoIdCard.isHidden = false
lblPhotoIdCard.isHidden = false
let mDatePicker: UIDatePicker = UIDatePicker()
mDatePicker.datePickerMode = .date
sender.inputView = mDatePicker
mDatePicker.addTarget(self, action: #selector(birthDateChanged(sender:)), for: .valueChanged)
}else {
btnPhotoIdCard.isHidden = true
lblPhotoIdCard.isHidden = true
}
}
#objc func birthDateChanged(sender: UIDatePicker) {
let mDateFormatter = DateFormatter()
mDateFormatter.locale = Locale(identifier: "en_US_POSIX")
mDateFormatter.dateFormat = "dd MMMM yyyy"
self.txfBirthDate.text = mDateFormatter.string(from: sender.date)
}
thats just show date picker but i want add button on the right side text field and if user click it can show keyboard and text it to textfield
I'm sorry for my english
You've to try this
class yourViewController: UIViewController,UITextFieldDelegate
{
var screenHeight = UIScreen.main.bound.height
var screenWidth = UIScreen.main.bound.width
var dateFormatter = DateFormatter()
var doneButton = UIBarButtonItem()
var cancelButton = UIBarButtonItem()
var timeValueLabel = UIBarButtonItem()
var datePicker : UIDatePicker!
var toolBarDtPicker = UIToolbar()
//MARK:- Life Cycle
override func viewDidLoad() {
super.viewDidLoad()
yourTxtField.delegate = self
}
//TextField Editing Begin Function
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool
{
self.hideKeyboard()
textField.inputView = nil
textfieldEding(sender: textField)
return false
}
//To Hide Keyboard
#objc func hideKeyboard()
{
UIApplication.shared.sendAction(#selector(UIApplication.resignFirstResponder), to: nil, from: nil, for: nil)
}
//DatePicker Function
func datePickerView()
{
if ((self.datePicker != nil) && (self.datePicker.superview != nil))
{
datePicker.removeFromSuperview()
}
self.datePicker = UIDatePicker(frame: CGRect(x: 0, y:
UIScreen.main.bounds.height-200, width: UIScreen.main.bounds.width, height: 200))
self.datePicker.backgroundColor = UIColor.white
self.datePicker.addTarget(self, action: #selector(dateHandler), for: .valueChanged)
self.view.addSubview(datePicker)
toolBarDtPicker = UIToolbar(frame: CGRect(x: 0, y: screenHeight-230, width: screenWidth, height: 30))
toolBarDtPicker.barStyle = .default
toolBarDtPicker.isTranslucent = false
toolBarDtPicker.barTintColor = UIColor.blue
toolBarDtPicker.sizeToFit()
// Adding Button ToolBar
doneButton = UIBarButtonItem(title: "Done", style: .plain, target: self, action: #selector(doneClick))
let spaceButton = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
timeValueLabel = UIBarButtonItem(title: " ", style: .plain, target: self, action: nil)
let spaceButton2 = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
cancelButton = UIBarButtonItem(title: "Cancel", style: .plain, target: self, action: #selector(cancelClick))
doneButton.tintColor = UIColor.white
timeValueLabel.tintColor = UIColor.white
cancelButton.tintColor = UIColor.white
toolBarDtPicker.setItems([cancelButton, spaceButton, timeValueLabel, spaceButton2, doneButton], animated: true)
toolBarDtPicker.isUserInteractionEnabled = true
self.view.addSubview(toolBarDtPicker)
self.toolBarDtPicker.isHidden = false
datePicker.datePickerMode = .date
dateFormatter.dateStyle = .medium
dateFormatter.dateFormat = "EEEE dd MMM yyyy"
}
#objc func textfieldEding(sender: UITextField)
{
datePickerView()
}
#IBAction func showKeyBoard(_ sender: UIButton!)
{
self.yourTxtField.becomeFirstResponder()
}
//MARK: DatePicker
#objc func dateHandler(sender: UIDatePicker)
{
dateFormatter.dateFormat = "EEEE dd MMM yyyy"
timeValueLabel.title = dateFormatter.string(from: sender.date)
}
#objc func doneClick() //DatePIcker Done Button
{
//Done Button Code
}
//MARK: Cancel Btn
#objc func cancelClick() //DatePIcker Cancel Button
{
if ((self.datePicker != nil) && (self.datePicker.superview != nil))
{
datePicker.removeFromSuperview()
}
toolBarDtPicker.removeFromSuperview()
}
}

app works fine in Simulator (Xcode 7.1.1) but not on iOS device

I have an app that calculates the difference between two dates. It works fine in the Simulator but not on iOS device (iPad using iOS 9).
When I press the 'calculate difference' buttons on the iPad nothing happens. In the Debug Area of Xcode I get the following error message:
2015-11-16 08:33:32.246 Date-to-Date Calculator[369:64113] <CATransformLayer: >0x17eb95e0> - changing property masksToBounds in transform-only layer, will have >no effect
2015-11-16 08:33:32.250
Date-to-Date Calculator[369:64113] <CATransformLayer: 0x17d8ea30> - changing >property masksToBounds in transform-only layer, will have no effect
2015-11-16 08:33:32.251
Date-to-Date Calculator[369:64113] <CATransformLayer: 0x17d95830> - changing >property masksToBounds in transform-only layer, will have no effect
I've searched this on SO and cannot find a solution.
Does anyone have any suggestions?
Thanks!
Please find View Controller code below:
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var startDateTextField: UITextField!
#IBOutlet weak var endDateTextField: UITextField!
#IBOutlet weak var resultNumberOfDays: UILabel!
#IBOutlet weak var resultWeeksAndDays: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor(red: 210/255, green: 213/255, blue: 220/255, alpha: 1.0)
let toolBar = UIToolbar(frame: CGRectMake(0, self.view.frame.size.height/6, self.view.frame.size.width, 40.0))
toolBar.layer.position = CGPoint(x: self.view.frame.size.width/2, y: self.view.frame.size.height-20.0)
toolBar.barStyle = UIBarStyle.BlackTranslucent
toolBar.tintColor = UIColor.whiteColor()
UIToolbar.appearance().barTintColor = UIColor(red: 0/255, green: 0/255, blue: 205/255, alpha: 1.0)
let todayBtn = UIBarButtonItem(title: "Today", style: UIBarButtonItemStyle.Plain, target: self, action: "tappedToolBarBtn:")
let okBarBtn = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.Plain, target: self, action: "donePressed:")
// let okBarBtn = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Done, target: self, action: "donePressed:")
let flexSpace = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: self, action: nil)
let label = UILabel(frame: CGRect(x: 0, y: 0, width: self.view.frame.size.width / 3, height: self.view.frame.size.height))
label.font = UIFont(name: "Helvetica Neue", size: 13)
label.backgroundColor = UIColor.clearColor()
label.textColor = UIColor.whiteColor()
label.text = "Select a Start Date"
label.textAlignment = NSTextAlignment.Center
let textBtn = UIBarButtonItem(customView: label)
toolBar.setItems([todayBtn,flexSpace,textBtn,flexSpace,okBarBtn], animated: true)
startDateTextField.inputAccessoryView = toolBar
let toolBar2 = UIToolbar(frame: CGRectMake(0, self.view.frame.size.height/6, self.view.frame.size.width, 40.0))
toolBar2.layer.position = CGPoint(x: self.view.frame.size.width/2, y: self.view.frame.size.height-20.0)
toolBar2.barStyle = UIBarStyle.BlackTranslucent
toolBar2.tintColor = UIColor.whiteColor()
UIToolbar.appearance().barTintColor = UIColor(red: 0/255, green: 0/255, blue: 205/255, alpha: 1.0)
let todayBtn2 = UIBarButtonItem(title: "Today", style: UIBarButtonItemStyle.Plain, target: self, action: "secondTappedToolBarBtn:")
let okBarBtn2 = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.Plain, target: self, action: "secondDonePressed:")
// let okBarBtn2 = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Done, target: self, action: "secondDonePressed:")
let flexSpace2 = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: self, action: nil)
let label2 = UILabel(frame: CGRect(x: 0, y: 0, width: self.view.frame.size.width / 3, height: self.view.frame.size.height))
label2.font = UIFont(name: "Helvetica Neue", size: 13)
label2.backgroundColor = UIColor.clearColor()
label2.textColor = UIColor.whiteColor()
label2.text = "Select an End Date"
label2.textAlignment = NSTextAlignment.Center
let textBtn2 = UIBarButtonItem(customView: label2)
toolBar2.setItems([todayBtn2,flexSpace2,textBtn2,flexSpace2,okBarBtn2], animated: true)
endDateTextField.inputAccessoryView = toolBar2
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func donePressed(sender: UIBarButtonItem) {
startDateTextField.resignFirstResponder()
}
func secondDonePressed(sender: UIBarButtonItem) {
endDateTextField.resignFirstResponder()
}
func tappedToolBarBtn(sender: UIBarButtonItem) {
let dateformatter = NSDateFormatter()
dateformatter.dateStyle = NSDateFormatterStyle.MediumStyle
dateformatter.timeStyle = NSDateFormatterStyle.NoStyle
startDateTextField.text = dateformatter.stringFromDate(NSDate())
startDateTextField.resignFirstResponder()
}
func secondTappedToolBarBtn(sender: UIBarButtonItem) {
let dateformatter = NSDateFormatter()
dateformatter.dateStyle = NSDateFormatterStyle.MediumStyle
dateformatter.timeStyle = NSDateFormatterStyle.NoStyle
endDateTextField.text = dateformatter.stringFromDate(NSDate())
endDateTextField.resignFirstResponder()
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
self.view.endEditing(true)
}
#IBAction func textFieldEditing(sender: UITextField) {
let datePickerView: UIDatePicker = UIDatePicker()
datePickerView.datePickerMode = UIDatePickerMode.Date
sender.inputView = datePickerView
datePickerView.addTarget(self, action: Selector("datePickerValueChanged:"), forControlEvents: UIControlEvents.ValueChanged)
}
#IBAction func secondTextFieldEditing(sender: UITextField) {
let datePickerView: UIDatePicker = UIDatePicker()
datePickerView.datePickerMode = UIDatePickerMode.Date
sender.inputView = datePickerView
datePickerView.addTarget(self, action: Selector("secondDatePickerValueChanged:"), forControlEvents: UIControlEvents.ValueChanged)
}
func datePickerValueChanged(sender: UIDatePicker) {
let dateFormatter = NSDateFormatter()
dateFormatter.dateStyle = NSDateFormatterStyle.MediumStyle
dateFormatter.timeStyle = NSDateFormatterStyle.NoStyle
startDateTextField.text = dateFormatter.stringFromDate(sender.date)
}
func secondDatePickerValueChanged(sender: UIDatePicker) {
let dateFormatter = NSDateFormatter()
dateFormatter.dateStyle = NSDateFormatterStyle.MediumStyle
dateFormatter.timeStyle = NSDateFormatterStyle.NoStyle
endDateTextField.text = dateFormatter.stringFromDate(sender.date)
}
#IBAction func calculateDays(sender: UIButton) {
if startDateTextField.text! == "" || endDateTextField! == "" {
let alert2 = UIAlertController(title: "Oops!", message: "Please Select a Start Date!", preferredStyle: UIAlertControllerStyle.Alert)
alert2.addAction(UIAlertAction(title: "Close", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert2, animated: true, completion: nil)
}
else {
let start = String(startDateTextField.text!)
let end = String(endDateTextField.text!)
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "MM-dd-yyyy"
guard let startDate = dateFormatter.dateFromString(start), endDate = dateFormatter.dateFromString(end) else {
// You don't have dates, show error(print("error"), do no nothing - your choice.
return
}
// let startDate:NSDate = dateFormatter.dateFromString(start)!
// let endDate:NSDate = dateFormatter.dateFromString(end)!
let calendar = NSCalendar.currentCalendar()
let components = calendar.components([.Day], fromDate: startDate, toDate: endDate, options: [])
let secondNewString = "\(components.day) days"
resultNumberOfDays.text = secondNewString
}
}
#IBAction func calculateWeeksAndDays(sender: UIButton) {
if startDateTextField.text! == "" || endDateTextField! == "" {
let alert2 = UIAlertController(title: "Oops!", message: "Please Select an End Date!", preferredStyle: UIAlertControllerStyle.Alert)
alert2.addAction(UIAlertAction(title: "Close", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert2, animated: true, completion: nil)
}
else {
let start = String(startDateTextField.text!)
let end = String(endDateTextField.text!)
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "MM-dd-yyyy"
guard let startDate = dateFormatter.dateFromString(start), endDate = dateFormatter.dateFromString(end) else {
// You don't have dates, show error(print("error"), do no nothing - your choice.
return
}
// let startDate:NSDate = dateFormatter.dateFromString(start)!
// let endDate:NSDate = dateFormatter.dateFromString(end)!
let calendar = NSCalendar.currentCalendar()
let components = calendar.components([.Day], fromDate: startDate, toDate: endDate, options: [])
let days = components.day
let weeks = components.day / 7
let weeksanddays = days % 7
let newString = "\(weeks) weeks + \(weeksanddays) days"
resultWeeksAndDays.text = newString
}
}
#IBAction func resetLabels(sender: AnyObject) {
resultNumberOfDays.text = ""
resultWeeksAndDays.text = ""
}
}
I'm willing to bet that your code, as it stands, doesn't "work fine" even in the simulator. For example when I tap on a text field the first time, the date picker doesn't show. I have to tap on the background screen to resignFirstResponder and then tap on the text field again to get the date picker to show.
The problem there is that you aren't instantiating the date picker and connecting it to the text field until after the text field has become the first responder, you need to do that before it becomes first responder. (For example in the viewDidLoad method.)

Resources