[英]Why The Navigation Title doesn't show up using SwiftUI?
I am learning SwiftUI
using apple's official tutorial: https://developer.apple.com/tutorials/swiftui/building-lists-and-navigation我正在使用苹果官方教程学习
SwiftUI
: https ://developer.apple.com/tutorials/swiftui/building-lists-and-navigation
Everything works perfectly until I try to show navigation title on an NavigationView
by calling .navigationBarTitle
.一切正常,直到我尝试通过调用
.navigationBarTitle
在NavigationView
上显示导航标题。
I have tried to refresh the live view, restart Xcode, but it still doesn't show up.我试图刷新实时视图,重新启动 Xcode,但它仍然没有出现。
here is my code:这是我的代码:
import SwiftUI
struct LandmarkList : View {
var body: some View {
NavigationView {
List(landmarkData) { landmark in
LandmarkRow(landmark: landmark)
}
}
.navigationBarItem(title: Text("Done"))
.navigationBarTitle(Text("Landmarks"))
}
}
#if DEBUG
struct LandmarkList_Previews : PreviewProvider {
static var previews: some View {
LandmarkList()
}
}
#endif
The Xcode looks like this: Xcode 看起来像这样:
According to the tutorial, it should show the navigation title but it doesn't in my case.根据教程,它应该显示导航标题,但在我的情况下没有。
Any idea why?知道为什么吗? Thanks!
谢谢!
.navigationBarTitle()
and .navigationBarItem()
are modifiers on the View
inside of the NavigationView
, not on the NavigationView
itself: .navigationBarTitle()
和.navigationBarItem()
是NavigationView
内部View
的修饰符,而不是NavigationView
本身:
struct LandmarkList: View {
var body: some View {
NavigationView {
List(landmarkData) { landmark in
LandmarkRow(landmark: landmark)
}
.navigationBarItem(title: Text("Done"))
.navigationBarTitle(Text("Landmarks"))
}
}
}
and if you think about it, this makes sense.如果你仔细想想,这是有道理的。 As the
View
within the NavigationView
changes, the new View
dictates what the title and contents of the navigation bar should be.随着
NavigationView
中的View
发生变化,新的View
决定了导航栏的标题和内容应该是什么。
NavigationView
is just a container around some content. NavigationView
只是围绕某些内容的容器。 Contents are changing when you navigating from page to page, but the NavigationView
persists.当您从一个页面导航到另一个页面时,内容会发生变化,但
NavigationView
仍然存在。
The point is that NavigationView
shows each view
's content when it shows it.关键是
NavigationView
在显示每个view
时会显示它的内容。 So it will encapsulate with the destination.所以它将与目的地封装在一起。
Finally, you should add all navigation modifiers inside the navigation (on the content)最后,您应该在导航内(在内容上)添加所有导航修饰符
struct Content: View {
var body: some View {
Text("Some. content")
.navigationBarItem(title: Text("Done"))
.navigationBarTitle(Text("Landmarks"))
}
}
struct Parent: View {
var body: some View {
NavigationView {
Content() // This contains navigation modifiers inside the view itself
}
}
}
If someone is looking for navigation title with tab button , this code may be helpful -如果有人正在寻找带有标签按钮的导航标题,此代码可能会有所帮助 -
struct ContentView: View {
var body: some View {
NavigationView {
Text("SwiftUI")
.navigationTitle("Screen Title")
.toolbar {
Button("Right") {
print("Right button tapped!")
}
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.