如何将UIActivityViewController与SwiftUI的ScrollView集成?

时间:2019-12-20 23:18:53

标签: swift uikit swiftui

在一个项目中,我有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)
            }
        }
//    }
    }
}

1 个答案:

答案 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()
    }
}