【问题标题】:Change the tab selection color in TabBar SwiftUI在 TabBar SwiftUI 中更改选项卡选择颜色
【发布时间】:2019-12-27 04:12:15
【问题描述】:

我正在尝试更改 TabBar 中选定选项卡的颜色,但没有任何效果。我可以通过写来更改 TabBar backgroundColor

struct ContentView: View {
    init() {
        UITabBar.appearance().backgroundColor = UIColor.purple
    }
    var body: some View { 
    }
}

在swift中,我们设置了tintColor,它确实改变了选定标签的颜色。但是我需要为 swiftUI 做什么?

这是我的代码,

    TabView(selection: $selection) {
        AView()
            .tabItem {
                VStack {
                    Image(systemName: "bubble.left.and.bubble.right")
                    Text("A Tab")
                }
        }.tag(0)

        BView()
            .tabItem {
                VStack {
                    Image(systemName: "house")
                    Text("B Tab")
                }
        }.tag(1)

        CView()
            .tabItem {
                VStack {
                    Image(systemName: "circle.grid.3x3")
                    Text("C Tab")
                }
        }.tag(2)
    }

对此有什么帮助吗? 提前致谢!!

【问题讨论】:

    标签: ios swiftui tabbar


    【解决方案1】:

    使用accentColorhttps://developer.apple.com/documentation/swiftui/tabview/3368073-accentcolor

    TabView {
      // fill this out with your tabbed content
    }
    .accentColor(.orange)
    

    【讨论】:

    • 这也会影响View 中的元素,例如Buttons。
    • 这是个坏方法。这仅适用于系统图标。如果您提供自定义图标,这将无法正常工作。
    • 您可能没有正确设置您的renderingMode,或者您的图标没有使用应有的透明度。