Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to give DatePicker a minute interval in SwiftUI

Tags:

swift

swiftui

does anyone know how to give the minutes DatePicker an interval? I am trying to only display multiples of 15 (0, 15, 30, 45). Is this only yet possible by interfacing with UIKit?

I believe it's not using the in: ...Date() part, at least I couldn't think of a solution. Here is my snippet:

DatePicker(
                "",
                selection: $selectedDate,
                /*in: ...Date(),*/
                displayedComponents: .hourAndMinute
            )

Thanks so much!

like image 372
denninho Avatar asked Nov 21 '19 13:11

denninho


1 Answers

The solution is to configure using UIDatePicker.appearance before SwiftUI DatePicker is created.

Tested with Xcode 13.3 / iOS 15.4

demo

Main part is:

UIDatePicker.appearance().minuteInterval = 15

Complete code in here

like image 167
Asperi Avatar answered Sep 24 '22 21:09

Asperi