相关疑难解决方法(0)

SwiftUI 2.0 - TabView 选项卡栏颜色不遵循当前的配色方案(深色或浅色模式)

我拼命尝试让我的标签栏颜色尊重当前的配色方案。当应用程序启动时,颜色是正确的。但是,如果我切换深色和浅色模式,颜色不会切换回正确的颜色。始终应用灯光模式颜色。代码位于图像下方(针对演示进行了简化)。

具有正确颜色的深色模式标签栏

具有正确颜色的灯光模式选项卡栏

带有扭曲颜色的深色模式标签栏

颜色在目录中指定Assets.xcassets(任意/浅色/深色)。

在此输入图像描述


import SwiftUI

struct TabBarColorTest: View {
    
    @Environment(\.colorScheme) var colorScheme
    
    init() {
        UITabBar.appearance().isTranslucent = true
        UITabBar.appearance().tintColor = UIColor(named: "TabBarTint")
        UITabBar.appearance().unselectedItemTintColor = UIColor(named: "TabBarUnselected")
        UITabBar.appearance().barTintColor = UIColor(named: "TabBar")
        UITabBar.appearance().backgroundColor = UIColor(named: "TabBar")
    }
    
    var body: some View {
        TabView {
            
            Text("Zero")
                .tabItem {
                    Label("Zero", systemImage: "0.square.fill")
                }
            
            Text("One")
                .tabItem {
                    Label("One", systemImage: "1.square.fill")
                }
        }
        .onChange(of: colorScheme, perform: { value in
            UITabBar.appearance().isTranslucent = true
            UITabBar.appearance().tintColor = UIColor(named: "TabBarTint")
            UITabBar.appearance().unselectedItemTintColor = UIColor(named: "TabBarUnselected")
            UITabBar.appearance().barTintColor = UIColor(named: "TabBar")
            UITabBar.appearance().backgroundColor = …
Run Code Online (Sandbox Code Playgroud)

tabbar tabview swiftui

6
推荐指数
1
解决办法
4212
查看次数

标签 统计

swiftui ×1

tabbar ×1

tabview ×1