1

我的问题似乎很简单。但我只是花了几个小时寻找解决方案!我只需要使状态栏的背景颜色符合我的UINavigationBar背景颜色(本示例中为红色)。

状态栏颜色与 NavigationBar 的颜色不符

我写了一个最小的代码示例来重现我的问题并帮助你给我一个解决方案......

TestApp.swift

import SwiftUI

@main
struct TestApp: App {
    @Environment(\.scenePhase) private var phase

    var body: some Scene {
        WindowGroup {
            ContentView()
        }
        .onChange(of: phase) { newPhase in
            switch newPhase {
            case .active:
                UINavigationBar.appearance().backgroundColor = UIColor.red
                break
            default:
                break
            }
        }
    }
}

内容视图.swift

struct DetailView1: View {

    var body: some View {
        Text("Detail view 1")
    }
}

struct ContentView: View {
    @State private var isDisplayDetailView1 = true

    var body: some View {
        NavigationView {
            List {
                Section {
                    NavigationLink(destination: DetailView1(), isActive: $isDisplayDetailView1) {
                        Text("Go to detail view 1")
                    }
                }
            }
            .navigationBarTitle("Menu")
            .listStyle(GroupedListStyle())
        
            DetailView1()
        }
    }
}

显然,我试图添加edgesIgnoringSafeArea(.top)到我的NavigationView视图(以及List一个,DetailView1一个,......),但它并没有改变任何东西......请帮助我,我不明白问题出在哪里!

4

1 回答 1

0

你有没有使用的原因ScenePhase

您可以在以下位置添加此代码file scope

extension UINavigationController {
    override open func viewDidLoad() {
        super.viewDidLoad()
        
        let standard = UINavigationBarAppearance()
        standard.backgroundColor = .blue
        
        let compact = UINavigationBarAppearance()
        compact.backgroundColor = .green
        
        let scrollEdge = UINavigationBarAppearance()
        scrollEdge.backgroundColor = .red
        
        navigationBar.standardAppearance = standard
        navigationBar.compactAppearance = compact
        navigationBar.scrollEdgeAppearance = scrollEdge
    }
}
于 2020-10-14T14:35:45.497 回答