【问题标题】:Change default blue color TabBar更改默认蓝色 TabBar
【发布时间】:2021-03-21 04:22:25
【问题描述】:

使用TabView.tabItem 创建TabBar 时,默认颜色为:未点击图像时为灰色,点击时为蓝色。这是一个示例代码。

struct TabBar: View{
@State var current = 0
var body: some View{
    TabView(selection: $current) {
        
         View0()
            .tag(0)
            .tabItem {
                Image(systemName: "anySystemImageName")
                Text("")
            }
         View3()
            .tag(1)
            .tabItem {
                Image(systemName: "anySystemImageName")
                Text("")
            }
         View2()
            .tag(2)
            .tabItem {
                Image(systemName: "anySystemImageName")
                Text("")
            }
            View3()
            .tag(3)
                .tabItem {
                    Image(systemName: "anySystemImageName")
                    Text("")
                }
  
    }
}
}

如何使用自定义颜色?

【问题讨论】:

    标签: swift swiftui tabbar tabview


    【解决方案1】:

    .accentColor是改变颜色的关键字

    struct ContentView: View{
        @State var current = 0
        var body: some View{
            TabView(selection: $current) {
                Text("View 1")
                    .tag(0)
                    .tabItem {
                        Image(systemName: "circle")
                        Text("")
                    }
                    
                Text("View 1")
                    .tag(1)
                    .tabItem {
                        Image(systemName: "circle")
                        Text("")
                    }
                Text("hallo")
                    .tag(2)
                    .tabItem {
                        Image(systemName: "circle")
                        Text("")
                    }
                
                Text("hallo")
                    .tag(3)
                    .tabItem {
                        Image(systemName: "circle")
                        Text("")
                    }
            }.accentColor(.red) //<< here
        }
    }
    

    【讨论】:

      【解决方案2】:

      你可以使用强调色,比如

          TabView(selection: $current) {
              
             // .. other code
        
          }.accentColor(.red)      // << here !!
      

      【讨论】:

        猜你喜欢
        • 2015-11-07
        • 1970-01-01
        • 2012-01-26
        • 2012-10-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多