从 SwiftUI 视图呈现 UIActivityViewController [英] Presenting UIActivityViewController from SwiftUI View

查看:48
本文介绍了从 SwiftUI 视图呈现 UIActivityViewController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从 SwiftUI 视图呈现一个 UIActivityViewController(共享表).我创建了一个名为 ShareSheet 的视图,符合 UIViewControllerRepresentable 来配置 UIActivityViewController,但事实证明,实际呈现它并不是那么简单.

I'm trying to present a UIActivityViewController (share sheet) from a SwiftUI View. I created a view called ShareSheet conformed to UIViewControllerRepresentable to configure the UIActivityViewController, but it's turning out to be not as trivial to actually present this.

struct ShareSheet: UIViewControllerRepresentable {
    typealias UIViewControllerType = UIActivityViewController

    var sharing: [Any]

    func makeUIViewController(context: UIViewControllerRepresentableContext<ShareSheet>) -> UIActivityViewController {
        UIActivityViewController(activityItems: sharing, applicationActivities: nil)
    }

    func updateUIViewController(_ uiViewController: UIActivityViewController, context: UIViewControllerRepresentableContext<ShareSheet>) {

    }
}

通过 .sheet 天真地这样做会导致以下结果.

Doing so naively via .sheet leads to the following.

.sheet(isPresented: $showShareSheet) {
    ShareSheet(sharing: [URL(string: "https://example.com")!])
}

有没有办法像通常呈现的那样呈现它?比如覆盖一半的屏幕?

Is there a way to present this like it's usually presented? As in covering half the screen?

推荐答案

希望能帮到你,

struct ShareSheetView: View {
    var body: some View {
        Button(action: actionSheet) {
            Image(systemName: "square.and.arrow.up")
                .resizable()
                .aspectRatio(contentMode: .fit)
                .frame(width: 36, height: 36)
        }
    }
    
    func actionSheet() {
        guard let data = URL(string: "https://www.apple.com") else { return }
        let av = UIActivityViewController(activityItems: [data], applicationActivities: nil)
        UIApplication.shared.windows.first?.rootViewController?.present(av, animated: true, completion: nil)
    }
}

这篇关于从 SwiftUI 视图呈现 UIActivityViewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆