我正在显示UIImagePickerController
以选择图像。我的代码很简单:
private lazy var imagePicker: UIImagePickerController = {
let picker = UIImagePickerController()
picker.navigationBar.isTranslucent = false
return picker
}()
func presentPicker() {
imagePicker.sourceType = .photoLibrary
imagePicker.modalPresentationStyle = .fullScreen
present(self.imagePicker, animated: true, completion: nil)
}
我将picker.navigationBar.isTranslucent = false
设置为在选择器控制器中具有不透明的导航栏。不幸的是,这在iOS 13上不起作用,并且导航和状态栏是透明的。
部分解决方案:
private func setOpaqueNavigationiOS13() {
UINavigationBar.appearance().backgroundColor = .white
}
private func resetNavigationiOS13() {
UINavigationBar.appearance().backgroundColor = .clear
}
我调用上述功能,以使导航栏不透明并在关闭选择器时将其重置。这使导航栏不透明,但是状态栏是透明的。我也可以实施hack,使状态栏不透明,但是我想应该有一个更简单的解决方案。
编辑:我还尝试通过新的UINavigationBarAppearance设置导航栏的外观:
if #available(iOS 13.0, *) {
let appearance = UINavigationBarAppearance()
appearance.configureWithOpaqueBackground()
appearance.backgroundColor = .white
UINavigationBar.appearance().standardAppearance = appearance
UINavigationBar.appearance().compactAppearance = appearance
UINavigationBar.appearance().scrollEdgeAppearance = appearance
}
或:
if #available(iOS 13.0, *) {
let appearance = UINavigationBarAppearance()
appearance.configureWithOpaqueBackground()
appearance.backgroundColor = .white
imagePicker.navigationBar.standardAppearance = appearance
imagePicker.navigationBar.compactAppearance = appearance
imagePicker.navigationBar.scrollEdgeAppearance = appearance
}
有人想出解决办法吗?谢谢
[在iOS 13中,自定义导航栏外观的正确方法是通过UIBarAppearance体系结构(导航栏的standardAppearance
等)。您可以将其直接应用于导航栏,也可以使用UINavigationBar外观代理。