我一直在尝试在新应用程序中实现照片选择功能。我当前的方法是使用嵌入 UIViewControllerRepresentable 中的 PHPickerViewController 来显示在 swiftUI 视图中。
这是我的 makeUIViewController 函数。
func makeUIViewController(context: Context) -> PHPickerViewController {
var configuration = PHPickerConfiguration()
configuration.filter = filter
configuration.selectionLimit = limit
let controller = PHPickerViewController(configuration: configuration)
controller.delegate = context.coordinator
return controller
}
Run Code Online (Sandbox Code Playgroud)
它位于名为的结构内PhotoPicker:
struct PhotoPicker: UIViewControllerRepresentable {
Run Code Online (Sandbox Code Playgroud)
是的,所有这些。让我解释一下,PickerView总是显示的,它不是弹出窗口,因此不需要取消按钮。正如您所看到的,也没有完成按钮。这是因为只需要选择一张图像,所以当用户点击图像时,会立即调用选择新图像的事件。无需用户确认。然后关于搜索栏,我并不是真的想要它,我只是希望用户选择一张照片,最后照片和相册之间的小切换在我的情况下也不是真正必要的。
我尝试了很多不同的方法,包括尝试在makeUIViewController. 这些选项例如:
controller.navigationController?.setToolbarHidden(true, animated: false)
controller.navigationController?.setNavigationBarHidden(true, animated: false)
Run Code Online (Sandbox Code Playgroud)
我还尝试在 SwiftUI 主体中调用视图修饰符:
PhotoPicker(filter: .images, limit: 1) { (results) in
}
.navigationBarTitle("")
.navigationBarHidden(true)
.statusBar(hidden: true)
.navigationBarBackButtonHidden(true)
Run Code Online (Sandbox Code Playgroud)
但同样,它们似乎都不起作用。所以这就是我在这里问的原因,因为似乎我尝试了一切,但没有任何效果......