1, use the storyboard to create a date selection control
First we add a UIDatePicker control and a button directly to the Main.Storyboard. This button is used to pop up a prompt box to display the currently selected date and time.
At the same time, use IBOutlet in ViewController.swift to establish the association between the control and the event. The specific code is as follows:
Import UIKit
Class ViewController: UIViewController {
@IBOutlet var dpicker: UIDatePicker!
@IBOutlet var btnshow:UIButton!
Override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func showClicked(_ sender:UIButton)
{
Let date = dpicker.date
// Create a date formatter
Let dformatter = DateFormatter()
// Set the format string for the date formatter
dformatter.dateFormat = "yyyy year MM month dd day HH:mm:ss"
// format date and time using date formatter
Let datestr = dformatter.string(from: date)
Let message = "The date and time you selected is: \(datestr)"
// Create a UIAlertController object (message box) and display the date and time selected by the user through the message box
Let alertController = UIAlertController(title: "current date and time",
Message: message,
preferredStyle: .alert)
Let cancelAction = UIAlertAction(title: "OK", style: .cancel, handler: nil)
alertController.addAction(cancelAction)
Self.present(alertController, animated: true, completion: nil)
}
}
Original from: www.hangge.com Reprinted please keep the original link: https://www.hangge.com/blog/cache/detail_547.html
2, pure code creation date selection control
Import UIKit
Class ViewController: UIViewController {
Override func viewDidLoad() {
super.viewDidLoad()
/ / Create a date selector
Let datePicker = UIDatePicker(frame: CGRect(x:0, y:0, width:320, height:216))
/ / Set the date selector area to Chinese, the date of the selector is displayed in Chinese
datePicker.locale = Locale(identifier: "zh_CN")
/ / Note: the method name inside the action needs to add a colon ":"
datePicker.addTarget(self, action: #selector(dateChanged),
For: .valueChanged)
self.view.addSubview(datePicker)
}
/ / Date selector response method
Func dateChanged(datePicker : UIDatePicker){
/ / Update reminder time text box
Let formatter = DateFormatter()
//date style
formatter.dateFormat = "yyyy year MM month dd day HH:mm:ss"
Print(formatter.string(from: datePicker.date))
}
Override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
3, date selection control text changed to Chinese
The text in the default date selection control is English. If you want to display Chinese, you need to set the area of the date selection control as follows.
/ / Set the date selector area to Chinese, the date of the selector is displayed in Chinese
datePicker.locale = Locale(identifier: "zh_CN")
4, change the control time selection mode
By default, the options in the date selection control have both date and time. We can modify the selection mode so that it has only one of them.
(1) Only date selection
datePicker.datePickerMode = .date
(2) only time selection
datePicker.datePickerMode = .time
5, modify the text color
UIDatePicker The default text color is black, but if we use a dark background (for example) it will be illegible. We can modify the text color by setValue.
For example, we change the text to white:
datePicker.setValue(UIColor.white, forKey: "textColor")