I have a Form containing DatePickers, such as:
When I run this, the date picker is displayed modally. How do I get it to expand inline in the form? I can't see any obvious way to do it.
Code Block swift DatePicker("Date of Birth", selection: $child.dob, displayedComponents: .date)
When I run this, the date picker is displayed modally. How do I get it to expand inline in the form? I can't see any obvious way to do it.
Thanks for showing your code. Now I understand what you described.
Or else, you can try datePickerStyle, as suggested by mtsrodrigues.
But you need to care, that actual style and behavior depends on:
If any styles does not provide the style and behavior in iOS 13 & 14 as you expect, you may need to write it yourself.
You may need some time to refine it, and may need more time when iOS 15 is out...
So, I'm not sure this would be the best solution for you.
One good way, is that accepting the default style as is. The default style may be the most frequently used style, that users of iOS 13 might be accustomed to the default style of iOS 13, users of iOS 14 to the style of iOS 14.in iOS 13, the DatePicker expands inline when the date value is touched. in iOS 14, instead of expanding inline, it displays modally instead.
Or else, you can try datePickerStyle, as suggested by mtsrodrigues.
But you need to care, that actual style and behavior depends on:
iOS versions (as you have found)
Whether the DatePicker is contained in a Form or not
If any styles does not provide the style and behavior in iOS 13 & 14 as you expect, you may need to write it yourself.
Code Block @State var showsDatePicker = false let dateFormatter: DateFormatter = { let df = DateFormatter() df.dateStyle = .medium return df }() var body: some View { NavigationView { Form { TextField("Name", text: $child.name) HStack { Text("Date of Birth") Spacer() Text("\(dateFormatter.string(from: child.dob))") .onTapGesture { self.showsDatePicker.toggle() } .padding(EdgeInsets(top: 2, leading: 10, bottom: 2, trailing: 10)) .background(showsDatePicker ? Color.clear : Color.gray) } if showsDatePicker { DatePicker("", selection: $child.dob, displayedComponents: .date) .datePickerStyle(WheelDatePickerStyle()) } } .navigationBarTitle("Add Child", displayMode: .inline) .navigationBarItems(leading: Button(action: self.onCancel) { Text("Cancel") }, trailing: Button(action: self.onSave) { Text("Save") }) } }
You may need some time to refine it, and may need more time when iOS 15 is out...
So, I'm not sure this would be the best solution for you.