I would like to present an action sheet on iPad using SwiftUI, but it crashes with the following error:
Terminating app due to uncaught exception 'NSGenericException',
reason: 'Your application has presented a UIAlertController ()
of style UIAlertControllerStyleActionSheet from _TtGC11AppAlpha17HostingControllerVS_11ContentView_
(<_TtGC11AppAlpha17HostingControllerVS_11ContentView_: 0x105702dc0>).
The modalPresentationStyle of a UIAlertController with this style is UIModalPresentationPopover.
You must provide location information for this popover through the alert controller's popoverPresentationController.
You must provide either a sourceView and sourceRect or a barButtonItem.
If this information is not known when you present the alert controller, you may provide it
in the UIPopoverPresentationControllerDelegate method -prepareForPopoverPresentation.'
My code:
.actionSheet(isPresented: $isShowingActionSheet) {
ActionSheet(
title: Text("Add a photo"),
message: Text("Please select a source"),
buttons: [
.cancel { },
.default(Text("Photo from library")) {
self.imageSource = .photoLibrary
self.isShowingImagePicker.toggle()
},
.default(Text("Take a picture")) {
self.imageSource = .camera
self.isShowingImagePicker.toggle()
}
]
)
}
I understand that I have to pass a source view for the action sheet to layout properly, but I don't have access to the underlying UIKit element. How should it be done?
Target: iPad
Xcode 11.2 beta 2 (11B44)