In einem Projekt habe ich eine ScrollViewUmgebung VStackvon Elementen, die jeweils eine Schaltfläche zum Auslösen der Aktivitätsansicht über das haben UIActivityViewController, aber die Aktivitätsansicht wird nicht angezeigt.
Ich habe das Projekt auf den folgenden Code reduziert, der die Aktivitätsansicht erfolgreich anzeigt. Wenn ich jedoch das auskommentiere ScrollView, wird die Aktivitätsansicht beim Drücken der "Open Activity View"Schaltfläche nicht mehr angezeigt .
Vielen Dank!
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)
}
}
// }
}
}