Swift 当 clipToBounds 为 true 时,UINavigationBar 背景图像不覆盖状态栏

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

我正在这样设置大标题导航栏的背景图片:

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
}

但是,图像溢出导航栏,如下所示:

我认为 clipsToBounds 可能有效,所以我在 viewDidLoad

结束时将其设置为 true
navigationController?.navigationBar.clipsToBounds = true

这样更好,但图像不会覆盖状态栏!如何让它覆盖状态栏而不溢出?

我们已经解决了这个问题:

我们在 ViewController 上设置了背景图片。这和我们在导航栏中使用的是一样的。

let imageView = UIImageView(image: .assetImage(.background))
imageView.contentMode = .topLeft
collectionView.backgroundView = imageView

设置导航栏的样式。

// General Styling
UINavigationBar.appearance().tintColor = .assetColor(.navigationBarTintColor)
        
// Bar Styling
let navBarAppearance = UINavigationBarAppearance()
navBarAppearance.shadowColor = .clear
navBarAppearance.backgroundColor = UIColor(patternImage: .assetImage(.background)!)
        
UINavigationBar.appearance().standardAppearance = navBarAppearance
UINavigationBar.appearance().scrollEdgeAppearance = navBarAppearance

此外,我们更改了 non-root 视图的导航栏的半透明:

if isRootview {
    self.navigationController?.navigationBar.isTranslucent = true
} else {
    self.navigationController?.navigationBar.isTranslucent = false
}