更改选项卡式视图栏颜色 SwiftUI

Ang*_*uck 19 swiftui

有谁知道如何更改选项卡式视图底部栏的背景颜色?

当我选择每个标签栏项目时,我已经设置了改变图标颜色的强调色。

我尝试将背景设置为一种颜色,但它不会改变背景,并尝试将背景设置为图像只是为了确定,但这也没有任何作用。

想知道我是否需要以某种方式专门访问底部栏,然后在其上设置一个属性?

Mar*_*ens 29

SwiftUI 1.0 - 使用命名颜色

结合barTintColorisTranslucent

出于某种原因,当我只使用barTintColor或什至使用backgroundColor. 我也必须包括在内isTranslucent

这是我命名的颜色:

命名颜色

设置只是 barTintColor

Just Bar Tint 颜色

(如您所见,它略有褪色)

设置只是 backgroundColor

只是背景颜色

(这会使条形变暗一点)

设置barTintColor&isTranslucent为 False

isTranslucent & barTintColor

这个组合对我来说是什么:

UITabBar.appearance().isTranslucent = false
UITabBar.appearance().barTintColor = UIColor(named: "Secondary")
Run Code Online (Sandbox Code Playgroud)

  • 这对我有用,而接受的答案却没有;我曾尝试将背景设为白色,但根据接受的答案,它保持灰色(尽管比以前稍浅)。谢谢! (3认同)

mum*_*umu 22

这是一个解决方案。您可以更改appearanceUITabBar 并更改 TabBar。

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

        return TabbedView {
            Text("This is tab 1").tag(0).tabItemLabel(Text("tab1"))
            Text("This is tab 2").tag(1).tabItemLabel(Text("tab2"))
            Text("This is tab 3").tag(2).tabItemLabel(Text("tab3"))
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 只是警告此答案依赖于 SwiftUI 使用“UITabBar”来表示“TabbedView”。这可能随时改变——例如,如果苹果创建了一个纯粹的快速标签栏。 (4认同)
  • 在这种情况下,我会更新答案,或者有人会更新。 (3认同)
  • @grey - 请参阅下面的答案。UITabBar.appearance().barTintColor = UIColor.blue (2认同)

Yar*_*arm 16

在 init() 添加 UITabBar.appearance().barTintColor = UIColor.blue

struct ContentView: View {

    @State private var selection = 1

    init() {
        UITabBar.appearance().barTintColor = UIColor.blue
    }

    var body: some View {
        TabView (selection:$selection){
            Text("The First Tab")
                .tabItem {
                    Image(systemName: "1.square.fill")
                    Text("First")
            }
            .tag(1)
            Text("Another Tab")
                .tabItem {
                    Image(systemName: "2.square.fill")
                    Text("Second")
            }.tag(2)
            Text("The Last Tab")
                .tabItem {
                    Image(systemName: "3.square.fill")
                    Text("Third")
            }.tag(3)
        }
        .accentColor(.white)
    }
}
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明


小智 7

它在最新版本中对我有用

var body: some View {
   
    TabView{
        
            Text("Zain ahmed")
            .font(.system(size: 30, weight: .bold, design: .rounded))
            .tabItem {
                Label("Home", systemImage: "house.fill")
            }
        
        Text("Bookmark Tab")
            .font(.system(size: 30, weight: .bold, design: .rounded))
            .tabItem {
                Label("Bookmark", systemImage: "bookmark.circle.fill")
            }
     
   
            
        
    }
    .onAppear() {
        UITabBar.appearance().backgroundColor = .lightGray
    }
   

}
Run Code Online (Sandbox Code Playgroud)