Change the Status Bar text color in SwiftUI
The App will pick the status bar text color based on the system mode of appearance. For Example, if your mobile is in the dark mode, the status bar text will appear in light content (i.e: in white text) or if it’s in the light mode, the status bar text color will appear in dark content (i.e in Black color).
So you can change your entire app’s status bar text color or you can change a specific views status bar color. The former can be achieved using the info.plist
and the other one can be achieved programmatically using the preferredColorScheme
modifier.
Change the Status bar text Color using info.plist
For changing the entire app’s status bar color all you have to do is add these two keys to the projects’ info.plist.
- “Status bar style” to “Dark Content“
- “View controller-based status bar appearance” to NO
and for adding the keys just right-click on the info.plist file and select Add row. As shown in the Screenshot below:
So we were able to change the status bar color without writing a single line of code. We simply set two info.plist keys which ended up changing the color.
Dark Mode:
Change the Status bar text Color programmatically of a specific view
So for changing the status bar text color programmatically we just have to add this modifier, preferredColorScheme to our main view. It will do the work for us. But it will only change the status bar color of the view in which this modifier is added.
struct ContentView: View { var body: some View { VStack { Image(systemName: "globe") .imageScale(.large) .foregroundColor(.accentColor) Text("Hello, world!").font(.largeTitle) } .preferredColorScheme(ColorScheme.light) } }
.preferredColorScheme(ColorScheme
.dark
)
for white text color.preferredColorScheme(ColorScheme.
light
)
for black text color
Light Mode:
Leave a Reply