简体   繁体   中英

Change Status Bar Color SwiftUI no UIHosting

I'm trying to change my status bar color dynamically without success, anyone has any idea how to change it ? I'm not using UIHosting controller so there is no AppDelegate or SceneDelegate fully using swiftUI:

@main
struct MyProjectApp: App{}

If i set ZStack {}.preferredColorScheme(.light) it only apply once, so if i go to another view and try to put ZStack {}.preferredColorScheme(.dark) it doesn't work.

I think i have try all the question here on stackoverflow, so if anyone have a definitive solution i appreciated.

@Updated code example

struct ContentA: View {
    var body: some View {
        NavigationView {
            ZStack {
                NavigationLink(destination: ContentB()) {
                    Text("Show Detail View")
                }.navigationBarTitle("Navigation")
            }.preferredColorScheme(.light)
        }
    }
}
struct ContentB: View {
    var body: some View {
        ZStack {
            Text("Hello ContetB")
                .padding()
                .foregroundColor(.green)
        }.preferredColorScheme(.dark)
    }
}

If you try to change StatusBar color using this way it doesn't work.

The colors within the app should be dynamic automatically, unless you set them specifically. You can toggle the .preferredColorScheme within the Previews section.

struct ContentView: View {
    var body: some View {
        Text("Hello, world!")
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
            .preferredColorScheme(.dark)
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM