4

I tried the solutions here: SwiftUI: Set Status Bar Color For a Specific View and here: SwiftUI: Set Status Bar Color For a Specific View

Both of these solutions utilize SceneDelegate, which obviously doesn't exist in SwiftUI 2/3. Ideally, I'd like the color to change for a specific view. It could be a modifier as they show in those posts or it could be based on a value I have in a Swift class that I call AppState:

class AppState: NSObject, ObservableObject {
  @Published var currentScreen: Screens = .view1
}

enum Screens {
  case view1
  case view2
  case view3
}

I'd like to make 'view2' in this case have a white status bar, not sure how to do this though--any help is much appreciated!

Update:

In my code, I have a Stack with a Color.black that has the .edgesIgnoringSafeArea(.all) property on this specific view but no others so I need to make the text white in this view, but black in the others...

Demo

Mojtaba Hosseini
  • 95,414
  • 31
  • 268
  • 278
nickcoding2
  • 142
  • 1
  • 8
  • 34
  • Check this : https://stackoverflow.com/a/66505930/14733292 – Raja Kishan Jul 08 '21 at 13:41
  • @RajaKishan I'm not trying to change the background of the status bar though, I'm trying to change the color of the actual text/images (AKA turn the time, location/wifi/battery usage icons to a different color, but only for one specific view) – nickcoding2 Jul 08 '21 at 15:01
  • 1
    Have you found a solution? I have the same problem – Niklas Oct 02 '21 at 07:18

2 Answers2

1

You can change the preferredColorScheme but this changes the whole scheme for the View not just the status bar. Fonts and backgrounds will switch too.

struct CustomStatusBarView: View {
    @StateObject var appState: AppState = AppState()
    var body: some View {
        ZStack{
            Color.gray.ignoresSafeArea()
            VStack{
                switch appState.currentScreen{
                    
                case .view1:
                    Text("view 1")
                case .view2:
                    Text("view 1").preferredColorScheme(.dark)
                case .view3:
                    Text("view 1")
                }
                Button("change view", action: {
                    appState.currentScreen = Screens.allCases.randomElement()!
                })
            }
        }
    }
}
lorem ipsum
  • 21,175
  • 5
  • 24
  • 48
  • I tried adding the .preferredColorScheme(.dark) modifier to the outermost View in ContentView and it's still only changing the stuff inside the safe area of the phone (AKA it's not changing the text color of the stuff in the status bar). I have no other colorScheme modifiers throughout the app...also, I'd like to use .colorScheme(.dark) instead because I need to force the text in the status bar to be white, but that doesn't change anything :/ – nickcoding2 Jul 08 '21 at 18:19
1

Please try adding the following key to the Info.plist file

<key>UIViewControllerBasedStatusBarAppearance</key> 
<true/>
Devanshi Modha
  • 359
  • 5
  • 8