4

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

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

2 回答 2

4

您可以使用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")})
                }
            }
        }
    }
}
于 2020-10-29T04:20:01.697 回答
0

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

init() {
    UIToolbar.appearance().barTintColor = UIColor.red
}

此代码有效:

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
}

取自Kristaps Grinbergs 的博客

于 2022-01-17T12:17:16.537 回答