PHPickerViewController 隐藏搜索栏和导航栏

Tit*_*uan 6 swift swiftui phpickerviewcontroller

我一直在尝试在新应用程序中实现照片选择功能。我当前的方法是使用嵌入 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)

但同样,它们似乎都不起作用。所以这就是我在这里问的原因,因为似乎我尝试了一切,但没有任何效果......

Cal*_*l S 0

您无法完全删除选择器的 UINavigationBar,但可以使用以下方法删除搜索栏和其他一些元素:

var config = PHPickerConfiguration(photoLibrary: .shared());    
if #available(iOS 17.0, *) {
    config.disabledCapabilities = [.search, .stagingArea, .collectionNavigation, .selectionActions];
}
let imagePicker = PHPickerViewController(configuration: config);
Run Code Online (Sandbox Code Playgroud)

编辑:还有“.photosPickerAccessoryVisibility”,但它仅在 SwiftUI 中可用,而不在 UIKit 中可用。文档在这里。