【问题标题】:SwiftUI: how to change NavigationView.toolbar background colorSwiftUI:如何更改 NavigationView.toolbar 背景颜色
【发布时间】:2021-02-11 10:25:10
【问题描述】:

知道如何将特定背景颜色应用到底部工具栏吗?

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") })
        }
    }
}

【问题讨论】:

  • 通常你可以做.background(Color.blue)
  • @aheze 不幸的是,如果我将它链接到工具栏,背景颜色不会改变

标签: swift swiftui swiftui-list swiftui-navigationview


【解决方案1】:

您可以使用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")})
                }
            }
        }
    }
}

【讨论】:

  • 这对我不起作用...
猜你喜欢
  • 2022-01-15
  • 1970-01-01
  • 2021-07-25
  • 2019-10-19
  • 1970-01-01
  • 1970-01-01
  • 2019-10-29
  • 2020-01-01
相关资源
最近更新 更多