NavigationBar backgroundColor 不着色状态栏背景

NavigationBar backgroundColor not coloring statusbar background

我在为导航栏着色时遇到了一些问题。我曾尝试使用一些不同教程中的不同方法,但似乎没有任何方法能按照我希望的方式工作。 我在我的 AppDelegate 中使用以下代码:

    let navAppeareance = UINavigationBarAppearance()

    navAppeareance.configureWithOpaqueBackground()
    navAppeareance.backgroundColor = .systemRed

    //Setup buttons
    let buttonDone = UIBarButtonItemAppearance(style: .done)
    buttonDone.normal.titleTextAttributes = [.foregroundColor: UIColor.white]
    navAppeareance.doneButtonAppearance = buttonDone

    let buttonPlain = UIBarButtonItemAppearance(style: .plain)
    buttonPlain.normal.titleTextAttributes = [.foregroundColor: UIColor.white]
    navAppeareance.buttonAppearance = buttonPlain

    //Set appearances
    UINavigationBar.appearance().standardAppearance = navAppeareance
    UINavigationBar.appearance().scrollEdgeAppearance = navAppeareance

    UINavigationBar.appearance().backgroundColor = .systemRed

以上代码产生以下结果: 我希望状态栏与导航栏的颜色相同,但它采用视图的背景颜色的颜色。

在 AppDelegate 中使用它

let navBarAppearnce = UINavigationBar.appearance()
navBarAppearnce.barTintColor = UIColor.red // the color you want