-
Re: ActionSheet crash on iPad
Jim Dovey Oct 17, 2019 2:24 PM (in response to ldidi)On the iPad, it seems you need to use .popover rather than .actionSheet. Sadly, ActionSheet() isn't a View type, so you can't just return your ActionSheet from the .popover content block. I've been playing around with it a bit, but I don't see an easy way to replicate the action sheet look-and-feel exactly. For example, I don't see any way to customize the popover's dimensions or background color at all; the view returned from the .popover builder block is simply placed inside an existing container of a static size (I'm guessing about 200x400 or something like that).
Here's the code I've used to play with this so far:
import SwiftUI import UIKit struct ContentView: View { @State var showingActionSheet = false @State var imageSource: UIImagePickerController.SourceType = .photoLibrary @State var showingPicker = false var body: some View { VStack { Button(action: { self.showingActionSheet.toggle() }) { Text("Show action sheet") } } .popover(isPresented: $showingActionSheet) { VStack { Text("Add a photo").bold().font(.largeTitle).foregroundColor(.secondary) Text("Select a source").font(.title).foregroundColor(.secondary) Divider() Button(action: { self.showingActionSheet = false }) { Text("Cancel").bold().font(.subheadline)//(.system(size: 18.0)) } Divider() Button(action: { self.imageSource = .photoLibrary self.showingPicker.toggle() self.showingActionSheet = false }) { Text("Photo from library").font(.system(size: 18.0)) } Divider() Button(action: { self.imageSource = .camera self.showingPicker.toggle() self.showingActionSheet = false }) { Text("Take a picture").font(.title) } } .padding(.vertical) .background(Color.green.opacity(0.6)) Spacer() } .padding(40) .background(Color.yellow) } } struct ContentView_Previews: PreviewProvider { static var previews: some View { ContentView() } }
I'm planning to file a bug report on this; I strongly suggest you do the same.
-
Re: ActionSheet crash on iPad
ldidi Oct 17, 2019 10:11 PM (in response to Jim Dovey)Good call. Filed bug FB7389876 on Feedback Assistant
-
-
Re: ActionSheet crash on iPad
mprudhom Oct 22, 2019 6:08 AM (in response to ldidi)We've run into this one as well (FB #FB7397761, FWIW). You can see our workaround at https://stackoverflow.com/questions/56910941/present-actionsheet-in-swiftui-on-ipad/58490096#58490096
-
Re: ActionSheet crash on iPad
Jim Dovey Oct 22, 2019 4:27 PM (in response to mprudhom)With the caveat that I've not actually pulled down your code to try it out yet: did you have any trouble persuading the popover to change its size? I couldn't get it to be a different size for love nor money.
-
-
Re: ActionSheet crash on iPad
adib Nov 11, 2019 3:16 AM (in response to ldidi)You could ask your own UIKit components to display the action sheet.
Use view controller containment to enclose your SwiftUI view. Have your own UIViewController subclass to enclose SwiftUI's view controller. In turn, have a property in the SwiftUI struct that is a closure that gets called to invoke the action sheet. Finally, initialize this closure from the enclosing UIViewController subclass to display that action sheet – in the closure you should have easy access to a UIView instance.