xcode 更改SwiftUI中tabItems的图像(图标)颜色

e5nszbig  于 2023-01-27  发布在  Swift
关注(0)|答案(3)|浏览(328)

如何在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()
    }
}
h7appiyu

h7appiyu1#

如果你想有不同的TabBar按钮颜色时,标签被选中,比我有理由相信,苹果提供的控制不会为你这样做。
你需要滚动你自己的控件。这是相当简单的。一个有三个选项卡的例子,见下面的代码。如果你使用固定数量的选项卡,这种方法可能对你有用。这些原则可以应用于使用@ViewBuilder等为更多和可变数量的选项卡构建控件。
我已经大致描述了股票选项卡栏的样式,如果这对您的用例很重要,您需要做一些改进以使其看起来完全相同。

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()
      }
}
isr3a4wc

isr3a4wc2#

要更改tabbar的色调颜色,您只需设置.accentColor()修饰符,以便TabView应用于项目。

ryevplcw

ryevplcw3#

尝试更改图像的渲染模式:在Assets.xcassets中选择图像,打开检查器视图并切换到属性检查器然后选择渲染为:模板图像
这对我很有效。

相关问题