我正在使用 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
}
我调用上述功能使导航栏变为不透明,并在取消选择器时重置它。这使得导航栏不透明,但状态栏仍然透明。我可以实现一个技巧来使状态栏也不透明,但我觉得应该有更简单的解决方案。 < p > 编辑: 我还尝试使用新的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
}
有人提出了解决方法吗?谢谢。
isTranslucent
属性不变。 - matt