Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Center Picker in View when .labelsHidden()

Tags:

swiftui

picker

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())
like image 317
Gerjan Avatar asked Nov 03 '19 14:11

Gerjan


2 Answers

DatePicker("Please enter a time", selection: $time, displayedComponents: .hourAndMinute)
    .labelsHidden()
    .datePickerStyle(WheelDatePickerStyle())
    .frame(minWidth: 0, maxWidth: .infinity, alignment: .center)
like image 167
Rudrank Riyam Avatar answered Sep 28 '22 01:09

Rudrank Riyam


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()
    }
like image 43
E.Coms Avatar answered Sep 28 '22 01:09

E.Coms