'Swift UINavigationBar background image does not cover status bar when clipToBounds is true

I'm setting the background image of a large title navigation bar like this:

override func viewDidLoad() {
    super.viewDidLoad()
    
    navigationController?.navigationBar.prefersLargeTitles = true
    
    self.title = "Nav bar"
    
    let largeTitleAppearance = UINavigationBarAppearance()
    
    largeTitleAppearance.configureWithOpaqueBackground()
    largeTitleAppearance.backgroundImage = UIImage(named: "kunal-shinde--f0YLss50Bs-unsplash")
    largeTitleAppearance.backgroundImageContentMode = .scaleAspectFill
    largeTitleAppearance.titleTextAttributes = [.foregroundColor: UIColor.white]
    largeTitleAppearance.largeTitleTextAttributes = [.foregroundColor: UIColor.white]
    
    navigationController?.navigationBar.standardAppearance = largeTitleAppearance
    navigationController?.navigationBar.scrollEdgeAppearance = largeTitleAppearance
}

However, the image overflows out of the navigation bar as shown here:

enter image description here

I thought clipsToBounds might work so I set it to true at the end of viewDidLoad

navigationController?.navigationBar.clipsToBounds = true

enter image description here

This is better, but the image does not cover the status bar! How can I get it to cover the status bar while not overflowing?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source