在一个项目中,我有ScrollView
包围着VStack
个项目,每个项目都有一个按钮来通过UIActivityViewController
触发“活动视图”,但是没有显示“活动视图”。
我将项目简化为以下代码,该代码成功显示了活动视图,但是当我取消注释ScrollView
时,按下"Open Activity View"
按钮时,活动视图不再出现。>
谢谢!
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)
}
}
// }
}
}
答案 0 :(得分:1)
这是行之有效的方法(在Xcode 11.2 / iOS 13.2上进行了测试)...不需要额外的包装器主机控制器,最好使用本机SwiftUI工具进行演示。
import SwiftUI
struct ActivityView: UIViewControllerRepresentable {
var url: String
@Binding var showing: Bool
func makeUIViewController(context: Context) -> UIActivityViewController {
let vc = UIActivityViewController(
activityItems: [NSURL(string: url)!],
applicationActivities: nil
)
vc.completionWithItemsHandler = { (activityType, completed, returnedItems, error) in
self.showing = false
}
return vc
}
func updateUIViewController(_ uiViewController: UIActivityViewController, context: Context) {
}
}
struct TestUIActivityView: View {
@State var showSheet = false
var body: some View {
ScrollView {
Group {
Button(action: {
self.showSheet.toggle()
}) {
Text("Open Activity View")
}
.sheet(isPresented: $showSheet) {
ActivityView(url: "https://www.wikipedia.org", showing: self.$showSheet)
}
}
}
}
}
struct TestUIActivityView_Previews: PreviewProvider {
static var previews: some View {
TestUIActivityView()
}
}