显示 UIImagePickerController 时更新导航栏背景

Updating Nav Bar background when displaying UIImagePickerController

在我的 AppDelegate 中,我将导航栏配置如下:

func setupNavBar() {
    let barAppearance = UINavigationBar.appearance()
    barAppearance.titleTextAttributes = [NSAttributedString.Key.foregroundColor: appRed]
    barAppearance.setBackgroundImage(UIImage(), for: UIBarMetrics.default)
    barAppearance.shadowImage = UIImage()
    barAppearance.isTranslucent = true
}

这很好用 - 直到我想显示我的 UIViewControllers 之一的 UIImagePickerController - 图像超出了栏,因为它是半透明的 - 我需要能够暂时制作导航条白色,然后在选择器控制器关闭时再次使其半透明:

我尝试通过在代码中添加最后两行来修复此问题:

 let pickerController = UIImagePickerController()
    pickerController.delegate = self
    pickerController.allowsEditing = false
    pickerController.mediaTypes = ["public.image"]
    pickerController.sourceType = .savedPhotosAlbum
    self.navigationController?.navigationBar.backgroundColor = .white
    self.navigationController?.navigationBar.isTranslucent = false
    self.present(pickerController, animated: true, completion: nil)

虽然这似乎不起作用。

试试这个:

let pickerController = UIImagePickerController()
pickerController.delegate = self
pickerController.allowsEditing = false
pickerController.mediaTypes = ["public.image"]
pickerController.sourceType = .savedPhotosAlbum
UINavigationBar.appearance().isTranslucent = false
self.present(pickerController, animated: true, completion: nil)

并在图像选择器上关闭

UINavigationBar.appearance().isTranslucent = true