如何将 UIActivityViewController 与 SwiftUI 的 ScrollView 集成?

Posted

技术标签:

【中文标题】如何将 UIActivityViewController 与 SwiftUI 的 ScrollView 集成?【英文标题】:How to integrate UIActivityViewController with SwiftUI's ScrollView? 【发布时间】:2019-12-20 23:18:53 【问题描述】:

在一个项目中,我有一个 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】:

这是可行的方法(使用 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()
    

【讨论】:

感谢您的回答。你知道如何输入原生变体吗?另请参阅以下问题:***.com/q/56700752/8642838您的解决方案导致工作表在整个显示器上运行

以上是关于如何将 UIActivityViewController 与 SwiftUI 的 ScrollView 集成?的主要内容,如果未能解决你的问题,请参考以下文章

如何将Ios文件上传到

Qt如何将文字变成图片?

如何将Bitmap保存为本地图片文件?

在MATLAB中如何将图导出

ASP如何将SQLSERVER数据导出到DBF(VF)

如何将CSV格式转换成JSON格式