如何在SwiftUI的tabItems中更改图像(图标)的颜色?
我尝试了很多事情,却无济于事...
这是我的测试代码:
import SwiftUI
struct TestTabviewIconColor: View {
var body: some View {
TabView {
Text("The First Tab")
.tabItem {
Image(systemName: "1.square.fill")
.foregroundColor(.red)
.accentColor(.red)
.colorMultiply(.red)
Text("First")
}
Text("Another Tab")
.tabItem {
Image(systemName: "2.square.fill")
Text("Second")
}
Text("The Last Tab")
.tabItem {
Image(systemName: "3.square.fill")
Text("Third")
}
}
.font(.headline)
}
}
struct TestTabviewIconColor_Previews: PreviewProvider {
static var previews: some View {
TestTabviewIconColor()
}
}
答案 0 :(得分:1)
要更改tabbar
的颜色,只需为TabView设置.accentColor()
修饰符即可应用于项目。
答案 1 :(得分:0)
我读这个问题是因为要为不同的标签使用不同的颜色。使用PreferenceKey机制可以实现。下面的代码使选项卡栏图标和文本分别为第一个标签红色,第二个绿色,第三个蓝色。 (我确实尝试使用.onAppear
来更改@State var tabColor
,但这在Preview中起作用,但在设备上不起作用。PreferenceKey机制在设备上以及在Preview中都起作用。)
(如果您希望所有图标的颜色都与@ Mac3n相同,并且还会看到先前回答的相同问题:Change the tab selection color in TabBar SwiftUI)。
import SwiftUI
struct TestTabviewIconColor: View {
@State var tabColor = Color.clear
var body: some View {
TabView() {
Text("The First Tab: \(tabColor.description)")
.tabItem {
Image(systemName: "1.square.fill")
Text("First")
}.preference(key: ColorPreferenceKey.self, value: Color.red)
Text("Another Tab: \(tabColor.description)")
.tabItem {
Image(systemName: "2.square.fill")
Text("Second")
}.preference(key: ColorPreferenceKey.self, value: Color.green)
Text("The Last Tab: \(tabColor.description)")
.tabItem {
Image(systemName: "3.square.fill")
Text("Third")
}.preference(key: ColorPreferenceKey.self, value: Color.blue)
}
.onPreferenceChange(ColorPreferenceKey.self, perform: { color in self.tabColor = color })
.accentColor(tabColor)
.font(.headline)
}
}
struct ColorPreferenceKey: PreferenceKey {
static var defaultValue: Color = Color.clear
static func reduce(value: inout Color, nextValue: () -> Color) {
value = nextValue()
}
}
struct TestTabviewIconColor_Previews: PreviewProvider {
static var previews: some View {
TestTabviewIconColor()
}
}
答案 2 :(得分:0)
如果您希望在选择选项卡时具有不同的TabBar按钮颜色,那么我有理由相信Apple提供的控件不会为您做到这一点。
您需要滚动自己的控件。这很简单。有关带有三个选项卡的示例,请参见下面的代码。如果您使用固定数量的标签,则此方法可能适合您。并且可以使用这些原理来使用@ViewBuilder等为更多和可变数量的选项卡构建控件。
我估算了库存TAB栏的样式。如果这对您的用例很重要,则需要进行一些细化以使其看起来完全相同。
struct TabLabel : View {
var text : String
var imageName : String
var color : Color
var body : some View {
VStack() {
Image(systemName: imageName)
Text(text).font(.caption)
}.foregroundColor(color)
}
}
struct TabButton : View {
@Binding var currentSelection : Int
var selectionIndex : Int
var label : TabLabel
var body : some View {
Button(action: { self.currentSelection = self.selectionIndex }) { label }.opacity(selectionIndex == currentSelection ? 0.5 : 1.0)
}
}
struct CustomTabBarView<SomeView1 : View, SomeView2 : View, SomeView3 : View> : View {
var view1 : SomeView1
var view2 : SomeView2
var view3 : SomeView3
@State var currentSelection : Int = 1
var body : some View {
let label1 = TabLabel(text: "First", imageName: "1.square.fill", color: Color.red)
let label2 = TabLabel(text: "Second", imageName: "2.square.fill", color: Color.purple)
let label3 = TabLabel(text: "Third", imageName: "3.square.fill", color: Color.blue)
let button1 = TabButton(currentSelection: $currentSelection, selectionIndex: 1, label: label1)
let button2 = TabButton(currentSelection: $currentSelection, selectionIndex: 2, label: label2)
let button3 = TabButton(currentSelection: $currentSelection, selectionIndex: 3, label: label3)
return VStack() {
Spacer()
if currentSelection == 1 {
view1
}
else if currentSelection == 2 {
view2
}
else if currentSelection == 3 {
view3
}
Spacer()
HStack() {
button1
Spacer()
button2
Spacer()
button3
}.padding(.horizontal, 48)
.frame(height: 48.0)
.background(Color(UIColor.systemGroupedBackground))
}
}
}
struct ContentView: View {
var body: some View {
let view1 = VStack() {
Text("The First Tab").font(.headline)
Image(systemName: "triangle").resizable().aspectRatio(contentMode: .fit).frame(width: 100)
}
let view2 = Text("Another Tab").font(.headline)
let view3 = Text("The Final Tab").font(.headline)
return CustomTabBarView(view1: view1, view2: view2, view3: view3)
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}