SwiftUI 2.0-选项卡视图选项卡栏颜色不';t尊重当前的配色方案(暗模式或亮模式)

SwiftUI 2.0-选项卡视图选项卡栏颜色不';t尊重当前的配色方案(暗模式或亮模式),swiftui,tabbar,tabview,Swiftui,Tabbar,Tabview,我拼命想让我的标签栏颜色符合当前的配色方案。 当应用程序启动时,颜色是正确的。但如果我切换黑暗和光明模式,颜色不会切换回正确的。始终应用灯光模式颜色。这些代码在图片下方提供(为演示简化) 颜色在Assets.xcsets目录(任意/浅/暗)中指定 通过将选项卡项“着色颜色”作为快捷UI修改器,并简化选项卡栏UIKIt配置的初始化,应该可以解决此问题。在Xcode 12.4上测试,最低目标为iOS 14 struct ContentView: View { init() {

我拼命想让我的标签栏颜色符合当前的配色方案。 当应用程序启动时,颜色是正确的。但如果我切换黑暗和光明模式,颜色不会切换回正确的。始终应用灯光模式颜色。这些代码在图片下方提供(为演示简化)

颜色在
Assets.xcsets
目录(任意/浅/暗)中指定



通过将选项卡项“着色颜色”作为快捷UI修改器,并简化选项卡栏UIKIt配置的初始化,应该可以解决此问题。在Xcode 12.4上测试,最低目标为iOS 14

struct ContentView: View {

  init() {
    UITabBar.appearance().barTintColor = .systemBackground
    UITabBar.appearance().unselectedItemTintColor = UIColor(named: "TabBarUnselected")
  }
  
  var body: some View {
    TabView {
      
      Text("Zero")
        .tabItem {
          Label("Zero", systemImage: "0.square.fill")
        }
      
      Text("One")
        .tabItem {
          Label("One", systemImage: "1.square.fill")
        }
    }
    .accentColor(Color("TabBarTint"))
  }
}

您是否应该通过Assets.xcsets中的颜色来解决问题?我的意思是,你想让颜色了解新的配色方案并做出改变,这是你唯一想要的方式吗?谢谢!删除UITabBar.appearance().isTranslucent=true、UITabBar.appearance().tintColor=UIColor(名为:“TabBarTint”)和UITabBar.appearance().backgroundColor=UIColor(.skipperTabBar)修复了该问题!
struct ContentView: View {

  init() {
    UITabBar.appearance().barTintColor = .systemBackground
    UITabBar.appearance().unselectedItemTintColor = UIColor(named: "TabBarUnselected")
  }
  
  var body: some View {
    TabView {
      
      Text("Zero")
        .tabItem {
          Label("Zero", systemImage: "0.square.fill")
        }
      
      Text("One")
        .tabItem {
          Label("One", systemImage: "1.square.fill")
        }
    }
    .accentColor(Color("TabBarTint"))
  }
}