【发布时间】:2021-12-01 07:53:18
【问题描述】:
我想将导航视图设置为始终为黑色,即使列表位于其下方。现在,在这种情况下,导航视图变为灰色。我想动态更改导航视图的颜色,这就是我使用 NavigationBarModifier 的原因,但由于这与此问题无关,因此我删除了执行此操作的函数。这是当前代码。
import SwiftUI
struct NavigationBarModifier: ViewModifier {
var backgroundColor: Binding<Color>
init(backgroundColor: Binding<Color>) {
self.backgroundColor = backgroundColor
}
func body(content: Content) -> some View {
ZStack{
content
VStack {
GeometryReader { geometry in
self.backgroundColor.wrappedValue
.frame(height: geometry.safeAreaInsets.top)
.edgesIgnoringSafeArea(.top)
Spacer()
}
}
}
}
}
extension View {
func navigationBarColor(_ bgColor: Binding<Color>) -> some View {
self.modifier(NavigationBarModifier(backgroundColor: bgColor))
}
}
struct ContentView: View {
@State private var bgColor: Color = .black
var body: some View {
NavigationView {
List {
Button {
} label: {
Text("Button")
}
Button {
} label: {
Text("Button")
}
Button {
} label: {
Text("Button")
}
} .navigationBarColor(self.$bgColor)
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
【问题讨论】: