在SwiftUI工作表内显示UIActivityViewController时出现警告

o8x7eapl  于 2022-10-31  发布在  Swift
关注(0)|答案(1)|浏览(198)

我使用UIActivityViewController来允许我的SwiftUI应用程序与其他Activity共享内容:

import Foundation
import SwiftUI

struct ShareSheet : UIViewControllerRepresentable{
    var items: [Any]

    func makeUIViewController(context: Context) -> UIActivityViewController {
        let ac = UIActivityViewController(activityItems: items, applicationActivities: nil)

        return ac
    }

    func updateUIViewController(_ uiViewController: UIActivityViewController, context: Context) {

    }
}

为了清楚起见,我将使用下面的简单代码:

import SwiftUI

struct ContentView: View {
    @State var showForImageShare: Bool = false

    var body: some View {
        VStack {
            Image(systemName: "globe")
                .imageScale(.large)
                .foregroundColor(.accentColor)
            Text("Hello, world!")
                .onTapGesture {
                    showForImageShare.toggle()
                }
        }
        .sheet(isPresented: $showForImageShare){
            let image = "Hello everybody"

            ShareSheet(items: [image])
        }
    }
}

我在控制台中收到以下警告:

2022-10-23 23:41:07.692216+0300 Test UIActivityViewController[92164:30374157] [LayoutConstraints] Changing the translatesAutoresizingMaskIntoConstraints property of a UICollectionReusableView that is managed by a UICollectionView is not supported, and will result in incorrect self-sizing. View: <_UIActivityContentFooterView: 0x15cd56340; baseClass = UICollectionReusableView; frame = (16 244.333; 361 52); layer = <CALayer: 0x600001ab8180>>

我使用Xcode 14,部署目标:16.0
您能帮我找到展示UIActivityViewController的最佳方式吗?

vdzxcuhz

vdzxcuhz1#

这样如何?

import SwiftUI

struct ContentView: View {
    @State private var isActivityViewPresented = false

    var body: some View {
        Button("Share Link") {
            self.isActivityViewPresented = true
        }
        .background(
            ActivityView(
                isPresented: $isActivityViewPresented,
                activityItmes: [URL(string: "https://stackoverflow.com/questions/74174661/warning-when-presenting-uiactivityviewcontroller-inside-a-swiftui-sheet")!]
            )
        )
    }
}

public struct ActivityView: UIViewControllerRepresentable {
    @Binding var isPresented: Bool
    public let activityItmes: [Any]
    public let applicationActivities: [UIActivity]? = nil

    public func makeUIViewController(context: Context) -> UIViewController {
        UIViewController()
    }

    public func updateUIViewController(_ uiViewController: UIViewController, context: Context) {
        let activityViewController = UIActivityViewController (
            activityItems: activityItmes,
            applicationActivities: applicationActivities
        )

        if isPresented && uiViewController.presentedViewController == nil {
            uiViewController.present(activityViewController, animated: true)
        }
        activityViewController.completionWithItemsHandler = { (_, _, _, _) in
            isPresented = false
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

相关问题