Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIDatePicker replacement for tvOS?

Tags:

tvos

Since UIDatePicker is not available in tvOS, what does one use to ask the user for date/time? Is there a new class that replaces UIDatePicker?

Thanks

like image 940
johnnyMac Avatar asked Nov 20 '15 22:11

johnnyMac


1 Answers

Since Apple does not provide a date picker for tvOS, there is no standard solution.

However, some possible steps would be:

  1. Design and implement a UI for manipulating the date components (= year, month, day, hour, minute).
  2. Keep track of the currently selected date, e.g. using a stored property. A date label can be updated using property observers (e.g. didSet).
  3. Implement methods for manipulating the selected date - component by component - and connect them to the UI.

Remember that dates and date calculations are complicated(!), so use the provided date concepts and methods - have a look at Calendar and DateComponents in the documentation.

There, you will find useful methods, for example:

  • range(of: Calendar.Component, in: Calendar.Component, for: Date) can be used to find the number of months in a year, days in a month (and so on).

  • dateComponents(Set<Calendar.Component>, from: Date) and date(from: DateComponents) can be used to convert from dates to date components and vice versa.

  • Further, isValidDate(in: Calendar) can be useful to check that date components form a valid date in a certain calendar.


Below is a link to the user interface (with UISegmentedControls) that I developed for my app, however, there are other solutions such as the one linked by ergoon.

User interface with segmented controls.

In my case, the date picker is presented modally after selecting a table view cell in an input form.

like image 176
Linnea Roennqvist Avatar answered Jan 02 '23 11:01

Linnea Roennqvist