I'm showing a SwiftUI DatePicker with the datePickerStyle of type WheelDatePickerStyle(). It's inside a Section, which is within a Form. As I'm showing a Section Header, I don't want to show the Picker's label. When choosing .labelsHidden(), however, the Picker just moves to the left and leaves some space to the left.
How can I either center the picker, or make sure that it takes up the full width of the Section / Form?
DatePicker("Please enter a time", selection: $time, displayedComponents: .hourAndMinute)
.labelsHidden()
.datePickerStyle(WheelDatePickerStyle())
DatePicker("Please enter a time", selection: $time, displayedComponents: .hourAndMinute)
.labelsHidden()
.datePickerStyle(WheelDatePickerStyle())
.frame(minWidth: 0, maxWidth: .infinity, alignment: .center)
You don't need the label. Just like the following:
DatePicker("", selection: $time, displayedComponents: .hourAndMinute)
// .labelsHidden()
.datePickerStyle(WheelDatePickerStyle())
Another simple way is to use HStack
with Spacer()
at both sides.
HStack{
Spacer()
DatePicker.init(selection: $time, displayedComponents: .hourAndMinute, label: {
EmptyView()
})
.labelsHidden().datePickerStyle(WheelDatePickerStyle())
Spacer()
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With