By default if you add a single Text
it's displayed as a Button
.
Then, to change its color you need to set the accentColor
of the NavigationView (as this button is displayed in the navigation bar):
struct ContentView: View {
var body: some View {
NavigationView {
Text("Test")
.toolbar {
ToolbarItem(placement: .navigationBarLeading) {
Text("Title")
}
}
}
.accentColor(.black)
}
}
If you don't want to change the accentColor
in the whole NavigationView you can do:
NavigationView {
VStack {
//...
}
.accentColor(.accentColor)
}
.accentColor(.black)
However, if you want this Text
to behave like a Text
and not like a Button
you can use the following hack:
ToolbarItem(placement: .navigationBarLeading) {
HStack {
Text("Title")
Text("")
}
.foregroundColor(.red)
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…