Why is the modifier .navigationBarTitle not applied to NavigationView?

Lin*_*rth 9 swift swiftui

In SwiftUI, I understand that modifiers are used to modify a view. When modifying a view, the modifier returns the view wrapped in _ModifiedContent.
When embedding my view inside a NavigationView and assigning a navigation bar title like this: (as seen in Apple's official tutorials)

NavigationView {
    Text("Hello, World!")
        .navigationBarTitle(Text("My first SwiftUI App"))
}
Run Code Online (Sandbox Code Playgroud)

...why is the modifier for the navigation bar title applied to Text, not to NavigationView?

Tay*_*lor 6

就像马丁链接到的一样navigationBarTitle只有当包含在里面时才有效NavigationViewSwiftUI Essentials演讲中约52:30对此进行了简要讨论。标题最终流向NavigationView,因此被推送的视图也可以提供自己的标题(例如,在〜54:00被推送的视图)。

如果标题NavigationView直接附加到标题上,则需要预先确定所有可能的标题,并将其从与标题关联的视图中删除。

它与UIViewControllernavigationItem属性相似,该属性附加在每个推入视图中,而不是附加在其UINavigationController本身上。


Ved*_*yar 5

因为.navigationBarTitle()NavigationView 内部 View 上的is 修饰符,所以它不在 NavigationView 上。的情况也是如此.navigationBarItem()。请参阅建筑物清单和导航