PHPickerViewController隐藏搜索栏和导航栏。

6

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

这段代码位于一个名为PhotoPicker的结构体内:

struct PhotoPicker: UIViewControllerRepresentable {

我想隐藏的部分是: enter image description here 是的,所有这些。让我解释一下,PickerView始终处于显示状态,它不是弹出窗口,所以不需要取消按钮。正如您所见,也没有完成按钮。因为只需选择一个图像,所以当用户点击图像时,立即调用选择新图像的事件,无需用户确认。关于搜索栏,我并不需要它,我只希望用户选择一张照片,最后,在我这种情况下,照片和相册之间的切换开关并不是必要的。
我尝试了很多不同的方法,包括在makeUIViewController中创建控制器时尝试设置选项。例如这些选项:
controller.navigationController?.setToolbarHidden(true, animated: false)
controller.navigationController?.setNavigationBarHidden(true, animated: false)

我也尝试在我的SwiftUI主体中调用视图修改器:

PhotoPicker(filter: .images, limit: 1) { (results) in
    
}
.navigationBarTitle("")
.navigationBarHidden(true)
.statusBar(hidden: true)
.navigationBarBackButtonHidden(true)

但是,再次尝试了所有方法似乎都没有用。因此我来这里问,因为看起来我已经尝试了所有方法都不起作用...


我正在尝试在UIKit中实现相同的功能,但迄今为止没有成功。应该选择自定义实现。 - undefined
你完全可以使用自定义实现,但这会牺牲性能(因为默认的实现已经非常优化)。而且它更容易在API变更时出现问题。 - undefined
我们曾经有一个定制的实现,但现在我们在iOS 14中遇到了一个问题,就是它只能分享应用程序中特定的照片,我们的定制控制器无法与有限选择配合工作,所以我在考虑尝试这个,但可能不会成功。 - undefined
嗯,看起来我们似乎遇到了同样的问题,如果你能给这个问题投票,我想它会更快得到答案... - undefined
有关这个问题有任何更新吗?在我的情况下,我在一些 iPhone 12 上遇到了一个错误,即搜索输入关闭了表单。这是不是与以下链接中描述的相同错误:https://www.hackingwithswift.com/forums/swiftui/uiimagepicker-sheet-disappears-when-search-field-takes-focus/6465 - undefined
1个回答

0
你无法完全移除选择器的UINavigationBar,但你可以使用以下方法移除搜索栏和其他一些元素:
var config = PHPickerConfiguration(photoLibrary: .shared());    
if #available(iOS 17.0, *) {
    config.disabledCapabilities = [.search, .stagingArea, .collectionNavigation, .selectionActions];
}
let imagePicker = PHPickerViewController(configuration: config);

编辑:还有一个名为 ".photosPickerAccessoryVisibility" 的属性,但它只适用于 SwiftUI 而不是 UIKit。文档在这里。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接