Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the size of DatePicker in SwiftUI

Tags:

ios

swift

swiftui

Does anybody know how to change the size of the default DatePicker in SwiftUI? I have been trying to provide a custom frame to resize the DatePicker but I could't see the expected outcome.

Thanks in advance!

Here is what I tried

DatePicker("", selection: $currentDate, displayedComponents: .hourAndMinute)
                              .labelsHidden()
                              .frame(width: 150, height: 80, alignment: .center)
like image 902
bona912 Avatar asked Feb 09 '20 14:02

bona912


People also ask

What is datepicker in SwiftUI?

SwiftUI: DatePicker & TimePicker Published by Kelvin Tan on December 18, 2019 DatePicker gives you the flexibility of either selecting only the date or both the date and time. It’s a useful tool if you would like to give the user’s input on either date or time.

How to display time view for datepicker?

We can change our example to display time view for DatePicker by simply replacing .date with .hourAndMinute We can limit the DatePicker to specific ranges of dates, allowing date selections only before or, after a certain date, or between two dates.

What is the datepicker tool?

DatePicker gives you the flexibility of either selecting only the date or both the date and time. It’s a useful tool if you would like to give the user’s input on either date or time.

What is the difference between automatic and compact date picker?

.automatic : sets DatePicker style based on system its being rendered. .compact : sets DatePicker style to compact display which renders date and time views that can be tapped to reveal calendar and time picker. .graphical : sets DatePicker style to display calendar view.


2 Answers

Just clip it at the end as below...

DatePicker("", selection: $currentDate, displayedComponents: .hourAndMinute)
    .labelsHidden()
    .frame(width: 150, height: 80, alignment: .center)
    .clipped()
like image 133
Asperi Avatar answered Oct 28 '22 05:10

Asperi


I added .scaleEffect() after the datepicker, and now I can achieve size scaling...

like image 21
icebarley Avatar answered Oct 28 '22 06:10

icebarley