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

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

最佳答案

您可以使用 UIToolbar 执行此操作外貌。使用 Xcode 12/iOS 14 测试。
demo

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

关于SwiftUI:如何更改 NavigationView.toolbar 背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64583275/

相关文章:

ios - 迪尔德 : Library not loaded SwiftUI when app runs on iOS 12 using @available(iOS 13. 0, *)

如果 authorizationStatus 被拒绝,SwiftUI 将用户定向到他们的设置页面

ios - 如何在 SwiftUI 中使列表反转

swiftui - 相对于另一个居中 View 定位 View

swift - 在选择器列表 SwiftUI 上添加搜索栏

ios - 如何从不继承 UIviewController 的类(从带有 class 的 swift 文件)加载 UIViewController?

swift - UITableView 未显示正确的行数

swiftui - 如何在 SwiftUI 中传递进度值(也许使用组合)?

ios - Swift 多个按钮通过相同的 segue 发送字符串

ios - 无效的二进制文件 - 无效的 swift 支持