问题描述
我正在尝试设置 NavigationView
的背景颜色.我正在尝试使用下面的代码(部分来自 SwiftUI 教程)添加一个 ZStack
.然而,除非我用 Spacer()
I'm trying to set the background color of a NavigationView
. I'm trying by adding a ZStack
using the code below (partly from the SwiftUI tutorial). Yet it's only ever white unless I replace NavigationView...
with Spacer()
var body: some View {
ZStack
{
NavigationView {
List {
Toggle(isOn: $userData.showFavoritesOnly) {
Text("Favourites")
}
ForEach(userData.landmarks) { landmark in
if !self.userData.showFavoritesOnly || landmark.isFavorite {
NavigationLink(destination: LandmarkDetail(landmark: landmark)) {
LandmarkRow(landmark: landmark)
}
}
}
}
.navigationBarTitle(Text("Landmarks"), displayMode: .large)
}
}.background(Color.blue.edgesIgnoringSafeArea(.all))
}
我可以设置单个列表项的颜色,但我希望整个背景显示为蓝色
I can set the individual list item color but i want the whole background to show blue
推荐答案
同UINavigationBar
.但由于目前还没有直接的 api,你可以使用 appearance
更改它:
It is same as UINavigationBar
. But since there is no direct api yet, you can change it using appearance
:
UINavigationBar.appearance().backgroundColor = .orange
UINavigationBar.appearance().tintColor = .green
UINavigationBar.appearance().barTintColor = .yellow
UINavigationBar.appearance().titleTextAttributes = [.foregroundColor: UIColor.red]
UINavigationBar.appearance().largeTitleTextAttributes = [.foregroundColor: UIColor.red]
你应该把它放在你确定编译器读起来像在 init()
方法中的地方.
You should put this somewhere that you sure the compiler reads like inside the init()
method.
请注意,其中一些将在 Xcode 11 beta 5 下无法运行.
这篇关于如何在 SwiftUI 中设置 NavigationView 背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!