I'm trying to put a DatePicker()
in a .confirmationDialog
, but it's not displaying. My overall goal is to try and replicate the Journal app (in iOS 17.2 beta), so I need this look at least. I'm completely stuck, so even a vague pointer or idea might help me get to the solution.
import SwiftUI
struct DatePickerInConfirmationDialog: View {
@State var showDialog = false
@State var date = Date.now
var body: some View {
Button("Open dialog") {showDialog = true}
.confirmationDialog("Set Custom Date", isPresented: $showDialog) {
DatePicker("Enter date", selection: $date)
.datePickerStyle(.compact)
.frame(height: 400)
} message: {
Text("Set Date")
}
}
}
#Preview {
DatePickerInConfirmationDialog()
}
DatePicker()
in the 'Journal' App (iOS 17.2 beta):
DatePicker()
in my app:
Here is an existing answer for how to show a
UIDatePicker
in an action sheet, in UIKit.Note that a
confirmationDialog
in SwiftUI doesn't necessarily show an action sheet. I think it could be presented as just an alert depending on size classes, so you might have to check the size classes if you want to 100% replicate the same behaviour.The solution adds a view to
UIAlertController.view
, which the documentation tells you explicitly not to do. There likely isn't a "proper" way to do this because of that, unless Apple adds dedicated APIs to do this in future iOS versions.We can port it to SwiftUI by using a hidden (zero-sized)
UIViewControllerRepresentable
to present theUIAlertController
.Usage:
Also consider adding a
@Binding
for the selected date, instead of getting it from thedoneClicked
closure.