Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the status bar text color in SwiftUI for each TabView Item?

Tags:

ios

swiftui

You can find many (UIKit) solutions to set the text color of the status bar for a SwiftUI view. Unfortunately, in my experience, these solutions do not seem to work satisfactorily for TabViews at runtime.

Found Solutions:

SwiftUI: Set Status Bar Color For a Specific View

How can I change the status bar text color per view in SwiftUI?

Using these solutions, you will quickly find that the text color is not always set correctly when trying to switch between tabs.

Unfortunately, Apple currently doesn't seem to have a direct solution for SwiftUI to change the UIStatusBarStyle for each view like it was possible to do with UIKit.

I just can't find a stable way to change the status bar text color for each tab at runtime.

enter image description here

like image 904
Peter Kreinz Avatar asked Oct 12 '25 16:10

Peter Kreinz


1 Answers

I read the other Solutions but did not try them. But the following works, and I also think it lets the other Solutions work.

  1. Go to your Info.plist and set UIViewControllerBasedStatusBarAppearance to NO
  2. From here on, I would assume that the solutions mentioned by you, would also work.

If you don't mind having a deprecation warning in your Code this would also be an option:

TabView {
    Text("1")
        .tabItem { Text("1") }
        .onAppear {
            UIApplication.shared.setStatusBarStyle(.lightContent, animated: false)
        }

    Text("2")
        .tabItem { Text("2") }
        .onAppear {
            UIApplication.shared.setStatusBarStyle(.darkContent, animated: true)
        }
}

But setStatusBarStyle is deprecated and therefore it might not be the best option.

like image 154
KonDeichmann Avatar answered Oct 14 '25 05:10

KonDeichmann