Skip to main content

UIDatePicker Example with UIToolBar | iOS | Swift

UIDatePicker object use to select the date and time, The UIDatePicker class have multiple rotate wheels and take value from that wheel and give one object of UIDatePicker and to get date from UIDatePicker object.

In this example, take one textFiled in which to open the datePicker using inputView. and adding toolbar in DatePicker.

Add the UITextField Delegate and drag it to view controller also.


Follow the below steps :

Step 1 :  Adding one textFiled named  textField_Date in ViewController and give IBOutlet connection and delegate. also take one UIDatePicker variable.


@IBOutlet weak var textField_Date: UITextField! var datePicker : UIDatePicker!

Step 2 : The function pickUpDate to create UIDatePicker with ToolBar. 

func pickUpDate(_ textField : UITextField){ // DatePicker self.datePicker = UIDatePicker(frame:CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: 216)) self.datePicker.backgroundColor = UIColor.white self.datePicker.datePickerMode = UIDatePickerMode.date textField.inputView = self.datePicker // ToolBar let toolBar = UIToolbar() toolBar.barStyle = .default toolBar.isTranslucent = true toolBar.tintColor = UIColor(red: 92/255, green: 216/255, blue: 255/255, alpha: 1) toolBar.sizeToFit() // Adding Button ToolBar let doneButton = UIBarButtonItem(title: "Done", style: .plain, target: self, action: #selector(ViewController.doneClick)) let spaceButton = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil) let cancelButton = UIBarButtonItem(title: "Cancel", style: .plain, target: self, action: #selector(ViewController.cancelClick)) toolBar.setItems([cancelButton, spaceButton, doneButton], animated: false) toolBar.isUserInteractionEnabled = true textField.inputAccessoryView = toolBar }

Step 3 : Adding two buttons method which which is in ToolBar. One is doneClick and other is cancelClick.

func doneClick() { let dateFormatter1 = DateFormatter() dateFormatter1.dateStyle = .medium dateFormatter1.timeStyle = .none textField_Date.text = dateFormatter1.string(from: datePicker.date) textField_Date.resignFirstResponder() } func cancelClick() { textField_Date.resignFirstResponder() }

Step 4 : Calling the pickUpDate function in UITextField delegate method.

func textFieldDidBeginEditing(_ textField: UITextField) { self.pickUpDate(self.textField_Date) }

Step 5 : outPut UIDatePicker with ToolBar in UITextField.

Comments

Post a Comment

Popular posts from this blog

Draggable Marker and Drawing the routes between them using Google Maps | IOS | Swift

This blog is about how we can make the marker draggable and get the current location from it and to draw the routes between them. For that we have to install the pods of Google maps from developer.google.com. Then we have to add the following    pod 'GoogleMaps'   pod 'GooglePlaces'   pod 'GooglePlacesAPI'   pod 'SwiftyJSON' And we have to save the pods into the pod file. Then, we have to install all the pods through terminal. Again we have to make the outlet for textfield and button as discussed below. 2 TextField and 3 Buttons. Hope you will enjoy the coding. Happy coding.! import UIKit import GoogleMaps import GooglePlaces import SwiftyJSON import Alamofire enum Location { case startLocation case destinationLocation } class ViewController: UIViewController , GMSMapViewDelegate ,  CLLocationManagerDelegate { @IBOutlet weak var googleMaps: GMSMapView ! @IBOutlet weak

Route between two places using Google Maps iOS SDK.

Its very interesting to have the Google maps  Firstly. We have to install the pods of Google maps. So, follow the steps for installing the pods of Google maps on developers.google.com 1. Create the pod file as on the site. 2. In Podfile add the pod of Alamofire and SwiftyJSON. 3. Save the pod file.      pod 'GoogleMaps'     pod 'GooglePlaces'     pod 'Alamofire' , '~> 4.2.0'     pod 'SwiftyJSON' 4. Install the pods. Here is the code for route. import UIKit import GoogleMaps import GooglePlaces import Alamofire import SwiftyJSON class GoogleViewController: UIViewController , GMSMapViewDelegate {     @IBOutlet weak var googleView: GMSMapView !          // AIzaSyC907BQBnrZK0UjA2zARtE6Mq7L__yqw5Q     override func viewDidLoad() {         super . viewDidLoad ()         GMSServices . provideAPIKey ( "AIzaSyC907BQBnrZK0UjA2zARtE6Mq7L__yqw5Q" )         GMSPlacesClient . pr

Cancellable Task with Dispatch Work Item | iOS | Swift

In some scenarios, we all want that the task we want to perform should be cancellable later on. For this kind of behaviour, we already have OperationQueue but the use of It seems difficult for beginners. Most of us didn't use OperationQueue so frequently and were only familiar with Grand Central Dispatch (GCD) . by performing UI operations in the main thread, Dispatch Group, etc. By the use of DispatachWorkItem , we can create some tasks that can be cancelled later on. Here is the example, let workItem = DispatchWorkItem {     // Here is our task     debugPrint ( "Task is to print some text..." ) } This is DispatchWorkItem containing a task to print something. To perform this task, we need to make use of DispatchQueue like this by adding delay if required. DispatchQueue . main . asyncAfter (deadline: . now () + . seconds ( 1 ), execute: workItem ) Here is the basic example to add a task and perform it using DispatchQueue. Now to cancel that we need to keep the referen