SwiftUI:如何更改 NavigationView.toolbar 背景颜色

Dan*_*e B 3 swift swiftui swiftui-list swiftui-navigationview

关于如何将特定背景颜色应用于底部工具栏的任何想法?

NavigationView {
    List {
        ....
    }
    .toolbar {
        ToolbarItem(placement: .bottomBar) {
            Button(action: { model.selectTab(tab: "ITEM1") }, label: { Text("ITEM1") })
        }
        ToolbarItem(placement: .bottomBar) {
            Button(action: { model.selectTab(tab: "ITEM2") }, label: { Text("ITEM2") })
        }
        ToolbarItem(placement: .bottomBar) {
            Button(action: { model.selectTab(tab: "ITEM3") }, label: { Text("ITEM3") })
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Jac*_*ack 16

iOS 16 及以上版本

在此输入图像描述

注意:使用 .toolbarBackground(.visible, for: .navigationBar) 栏背景的首选可见性。

详细教程在这里 - https://janeshswift.com/ios/swiftui/how-to-change-navigationview-colour-in-swiftui/

struct ContentView : View {
    var body: some View {
        NavigationStack {
            Text("Dashboard")
                .toolbar {
                    ToolbarItem(placement: .principal) {
                        Text("Cool Title")
                            .foregroundColor(.black)
                    }
                }
                .navigationBarTitleDisplayMode(.inline)
                .toolbarBackground(.red, for: .navigationBar)
                .toolbarBackground(.visible, for: .navigationBar)
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 对于 .toolbar 不起作用。 (2认同)
  • 此示例展示了 .navigationBar(它是顶部栏)。但当我尝试使用 .toolbar (底部栏)时它不起作用。 (2认同)

Asp*_*eri 6

您可以使用UIToolbar外观来做到这一点。使用 Xcode 12 / iOS 14 测试。

演示

struct DemoView: View {
    
    init() {
        UIToolbar.appearance().barTintColor = UIColor.red
    }
    
    var body: some View {
        NavigationView {
            List {
                Text("Item")
            }
            .toolbar {
                ToolbarItem(placement: .bottomBar) {
                    Button(action: { }, label: {Text("ITEM1")})
                }
                ToolbarItem(placement: .bottomBar) {
                    Button(action: { }, label: {Text("ITEM2")})
                }
                ToolbarItem(placement: .bottomBar) {
                    Button(action: { }, label: {Text("ITEM3")})
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 这对我不起作用...... (9认同)

Ale*_*huk 6

下面的代码对我不起作用:

init() {
    UIToolbar.appearance().barTintColor = UIColor.red
}
Run Code Online (Sandbox Code Playgroud)

这段代码的工作原理:

init() {
  let coloredAppearance = UINavigationBarAppearance()
  coloredAppearance.configureWithOpaqueBackground()
  coloredAppearance.backgroundColor = .systemRed
  coloredAppearance.titleTextAttributes = [.foregroundColor: UIColor.white]
  coloredAppearance.largeTitleTextAttributes = [.foregroundColor: UIColor.white]
  
  UINavigationBar.appearance().standardAppearance = coloredAppearance
  UINavigationBar.appearance().compactAppearance = coloredAppearance
  UINavigationBar.appearance().scrollEdgeAppearance = coloredAppearance
  
  UINavigationBar.appearance().tintColor = .white
}
Run Code Online (Sandbox Code Playgroud)

摘自Kristaps Grinbergs 的博客


Art*_*uro 5

iOS 16+

.toolbarBackground(
    .red,
    for: .navigationBar)
.toolbarBackground(.visible, for: .navigationBar)
Run Code Online (Sandbox Code Playgroud)

欲了解更多信息:

https://developer.apple.com/documentation/swiftui/tabview/toolbarbackground(_:for:)

  • 对于 .toolbar 不起作用。 (3认同)
  • 所有这些评论都假设使用“正常”工具栏。除了 UIToolbar.appearance().barTintColor = UIColor.red 之外,.bottomBar 似乎没有响应。@Arturo,如果您想为导航栏(顶部)或选项卡栏(最底部)着色,您的建议有效。但令人沮丧的是,对附加工具栏 .bottomBar 的控制很少 (3认同)