SwiftUI_visionOS_Note

DatePicker()

Jan 18, 2024

The DatePicker is a native SwiftUI component for selecting date and/or time.

You can implement it using various initializers and customize it with different DatePickerStyles.

Additionally, a DateFormatter can be used to effectively display dates in your visionOS application.

 
 
 

1. Choose DatePicker

  • Select the on with title & selection parenthesis (most general)
  • Bind the today’s date variable
 
 

2. Stylize the component

  • .tint() isn’t working in DatePicker
  • .datePickerStyle() has three styles: compact, graphical, wheel
 

3. Select displayed component

  • Select the one with title, selection, displayComponents
  • We can choose which components will show with [.date, .hourAndMinute]
    • Can delete .date to only show .hourAndMinute and vice versa
 
 

4. Add Closed Range

  • Select the one with in: ColsedRange<Date>.
  • Create a Date let with starting from year 2018 to Today
 

4.1 Edit the display component

  • Add displayedComponents: [.date, .hourAndMinute] to edit the diplay
 

5. Display the Date() into string

  • We can bring the Date() data into string
 
  • We have to format the date by creating a function
  1. It creates variable called dateFormatter with DateFormatter{} component
  1. which means, it has to return DateFormatter() by calling formatter
  1. Now, formatter is DateFormatter()
  1. formatter.dateStyle = .short → DateFormatter().dateStyle = .short
  1. return formatter
 
  • We change the description

to

You might also like

BlogPro logo
Made with BlogPro