TabView Light Mode UIColor Appears in Preview, not in Sim or Device

I am using Swift 5.3 and created a new "Multiplatform" app. I created custom color assets for light/dark modes but am having trouble getting the light mode background color to show on my TabView. The TabView dark mode background color appears just fine.

The light mode background color appears perfect in Xcode Preview, but when I run the app in the Sim or on my device, the Tab Bar background color is clear in light mode, but shows the correct color for dark mode.

I use the same color assets for coloring my NavigationView and both the light and dark mode colors appear correctly when the corresponding modes are selected.

Is there something obvious I am missing here with the latest SwiftUI 5.3 and color assets?

I am using the following code within .onAppear in the TabView

UITabBar.appearance().barTintColor = UIColor(named: "barBackground")

Note that "barBackground" is the custom color set I created to add a light/dark mode background colors to both the NavigationView and TabView.

Happy to furnish additional info if this isn't clear. Thanks!

Doug


Accepted Reply

Got the answer on another forum:

It is a bit late to do it in .onAppear, appearance should be changed definitely before affected view is created, so do it in init:

init() {
UITabBar.appearance().barTintColor = UIColor(named: "barBackground")
}

Worked like a champ!

Replies

Got the answer on another forum:

It is a bit late to do it in .onAppear, appearance should be changed definitely before affected view is created, so do it in init:

init() {
UITabBar.appearance().barTintColor = UIColor(named: "barBackground")
}

Worked like a champ!