Mac上的SwiftUI中的UIDocumentPickerViewController(macCatalyst)

因此,我一直在干预将一个小型SwiftUI iPad应用“移动”到Mac,并且使用UIDocumentPickerViewController遇到了一些麻烦。 我将UIDocumentPickerViewController包裹在UIViewControllerRepresentable中,如下所示:

struct DocumentPickerView: UIViewControllerRepresentable {
  func makeUIViewController(context: Context) -> UIDocumentPickerViewController {
    let documentPicker = UIDocumentPickerViewController(documentTypes: [(kUTTypeImage as String)],in: .import)
    return documentPicker
  }

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

  }
}

并显示如下:

struct ContentView: View {
@State var shows = false
var body: some View {
    Button(action: { self.shows.toggle() }) {
        Text("Select File")
    }
    .sheet(isPresented: self.$shows) {
        DocumentPickerView()
    }
  }
}

在iPad上一切正常,

Mac上的SwiftUI中的UIDocumentPickerViewController(macCatalyst)

但是在Mac上,UIDocumentPickerViewController并没有显示,我们得到了这个空白模态:

Mac上的SwiftUI中的UIDocumentPickerViewController(macCatalyst)

w406834009 回答:Mac上的SwiftUI中的UIDocumentPickerViewController(macCatalyst)

出于某种原因(我不知道更多的具体细节),以下对我有用(对于 Catalyst):

而不是使用 in: .import

let documentPicker = UIDocumentPickerViewController(
    documentTypes: [(kUTTypeImage as String)],in: .import
)

使用in: .open

let documentPicker = UIDocumentPickerViewController(
    documentTypes: [(kUTTypeImage as String)],in: .open
)

我没有检查该功能是否在 iOS 中维护,但也许您可以使用某种“标志”来确定是使用 .import 还是 .open

本文链接:https://www.f2er.com/3018357.html

大家都在问