'How to switch between static color to blur Tab Bar depending on Light/Dark Theme in SwiftUI

I tried to do this but it doesn't work properly

struct TabBar: View {
    @Environment(\.colorScheme) var colorScheme
    
    var body: some View {
        TabView(...) {
            ...
        }
        .onAppear{
            let tabBarAppearance = UITabBarAppearance()

            tabBarAppearance.backgroundColor = colorScheme == .light ? UIColor(named: "color") : .none
            tabBarAppearance.backgroundEffect = colorScheme == .light ? nil : UIBlurEffect(style: .systemThinMaterial)abBarAppearance.stackedItemPositioning = .centered

            UITabBar.appearance().standardAppearance = tabBarAppearance
            UITabBar.appearance().scrollEdgeAppearance = tabBarAppearance
        }
        .onChange(of: colorScheme){ [colorScheme] newColor in
            UITabBar.appearance().standardAppearance.backgroundEffect = newColor == .light ? nil : UIBlurEffect(style: .systemThinMaterial)
            UITabBar.appearance().standardAppearance.backgroundColor = newColor == .light ? UIColor(named: "color") : nil
            
        }
    }
}

Expected after switching themes: light theme dark theme



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source