In a project I have a ScrollView
surrounding a VStack
of items which each have a button to trigger the Activity View via the UIActivityViewController
, but the Activity View doesn't appear.
I've reduced the project to the following code, which successfully displays the Activity View, but when I uncomment the ScrollView
, the Activity View no longer appears when pressing the "Open Activity View"
button.
Thanks!
import SwiftUI
class UIActivityViewControllerHost: UIViewController {
var url: String = ""
var completionWithItemsHandler: UIActivityViewController.CompletionWithItemsHandler? = nil
override func viewDidAppear(_ animated: Bool) {
let vc = UIActivityViewController(
activityItems: [NSURL(string: url)!],
applicationActivities: nil
)
vc.completionWithItemsHandler = completionWithItemsHandler
present(vc, animated: true, completion: nil)
super.viewDidAppear(animated)
}
}
struct ActivityView: UIViewControllerRepresentable {
var url: String
@Binding var showing: Bool
func makeUIViewController(context: Context) -> UIActivityViewControllerHost {
let result = UIActivityViewControllerHost()
result.completionWithItemsHandler = { (activityType, completed, returnedItems, error) in
self.showing = false
}
return result
}
func updateUIViewController(_ uiViewController: UIActivityViewControllerHost, context: Context) {
uiViewController.url = url
}
}
struct ContentView: View {
@State var showSheet = false
var body: some View {
// ScrollView {
Group {
Button(action: {
self.showSheet.toggle()
}) {
Text("Open Activity View")
}
if showSheet {
ActivityView(url: "https://www.wikipedia.org", showing: $showSheet)
.frame(width: 0, height: 0)
}
}
// }
}
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…