我希望能够为SwiftUI中的NavBar和navigationBarItems设置自定义颜色。这是navigationBarItems的代码:
.navigationBarItems(leading: EditButton(), trailing: Button(action: {
self.showingAddScreen.toggle()
}) {
Image(systemName: "plus")
})
.sheet(isPresented: $showingAddScreen) {
NavigationView {
AddTaskView().environment(\.managedObjectContext, self.moc)
}
}
我用以下代码创建了一个扩展名,该扩展名在NavigationBar背景色上效果很好,但是它不会更改navigationBarItems的颜色-它们具有默认的蓝色按钮。
extension UINavigationController {
override open func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
let standardAppearance = UINavigationBarAppearance()
standardAppearance.backgroundColor = UIColor(red: 66/255, green: 116/255, blue: 147/255, alpha: 1.0)
let scrollEdgeAppearance = UINavigationBarAppearance()
scrollEdgeAppearance.backgroundColor = UIColor(red: 66/255, green: 116/255, blue: 147/255, alpha: 1.0)
let compactAppearance = UINavigationBarAppearance()
compactAppearance.backgroundColor = UIColor(red: 66/255, green: 116/255, blue: 147/255, alpha: 1.0)
navigationBar.standardAppearance = standardAppearance
navigationBar.scrollEdgeAppearance = scrollEdgeAppearance
navigationBar.compactAppearance = compactAppearance
navigationBar.tintColor = UIColor.white
}
}
更改NavigationBarItems颜色的正确方法是什么?
答案 0 :(得分:0)
我发现,虽然通用设置可能很方便,但实际上您可以为每个按钮设置.foregroundColor的样式:
.navigationBarItems(leading: EditButton().foregroundColor(Color.white), trailing: Button(action: {
self.showingAddScreen.toggle()
}) {
Image(systemName: "plus")
.foregroundColor(Color.white)
})
.sheet(isPresented: $showingAddScreen) {
NavigationView {
AddTaskView().environment(\.managedObjectContext, self.moc)
}
}