【发布时间】:2021-06-01 08:22:54
【问题描述】:
我正在拼命想让我的标签栏颜色尊重当前的配色方案。 当应用程序启动时,颜色是正确的。但是,如果我切换深色和浅色模式,颜色就不会切换回正确的颜色。始终应用灯光模式颜色。代码位于图片下方(为演示而简化)。
颜色在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 = UIColor(named: "TabBar")
})
}
}
struct TabBarColorTest_Previews: PreviewProvider {
static var previews: some View {
TabBarColorTest()
}
}
【问题讨论】:
-
您应该通过 Assets.xcassets 中的颜色来解决问题吗?我的意思是你希望 Colors 了解新的 ColorScheme 并做出改变,这是你想要的唯一方式吗?