mac 上 SwiftUI 中的 UIDocumentPickerViewController (macCatalyst) [英] UIDocumentPickerViewController in SwiftUI on mac (macCatalyst)

查看:76
本文介绍了mac 上 SwiftUI 中的 UIDocumentPickerViewController (macCatalyst)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我一直在干预将一个小型 SwiftUI iPad 应用程序移动"到 Mac,并且我在 UIDocumentPickerViewController 上遇到了一些速度障碍.我已经将 UIDocumentPickerViewController 包装在 UIViewControllerRepresentable 中,如下所示:

So I've been meddling with "moving" a small SwiftUI iPad app to the Mac, and I've hit a bit of a speed bump with UIDocumentPickerViewController. I have wrapped the UIDocumentPickerViewController in a UIViewControllerRepresentable like so :

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 上时,UIDocumentPickerViewController不显示,我们得到这个空白的模式:

But when on on the Mac, the UIDocumentPickerViewControllerdoesnt show and we get this blank modal:

推荐答案

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

For some reason (I'm not aware of further specific details), the following worked for me (for Catalyst):

而不是使用 in: .import

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

中使用:.open

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

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

I haven't checked if the functionality is maintained in iOS, but perhaps you can use some sort of "flag" to determine wether to use .import or .open

这篇关于mac 上 SwiftUI 中的 UIDocumentPickerViewController (macCatalyst)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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