NavigationView is one of the most important components of the SwiftUI application, enabling us to easily push and pop up screens to present information to users in a clear, layered way. NavigationView is usually used in the outermost layer of the page. You can add a title bar, page content, and a bottom navigation bar inside. NavigationView is a handy way to add a title bar to a page, assuming the page has only one “Hell World”

NavigationView { Text("Hello, World!" ). NavigationBarTitle (" I'm the navigationBarTitle ")}Copy the code

You can customize the title style by adding the displayMode parameter. There are three styles:

  1. The Large option displays large headings, which are useful for navigating the top-level view in the stack.
  2. The inline option displays the subtitle, which is useful for navigating secondary, third, or subsequent views in the stack.
  3. The Automatic option is the default option, and it uses any view that was used before.

We usually use inline style:

.navigationBarTitle("Navigation", displayMode: .inline)
Copy the code

If you want to add a button to the title bar, you can use the navigationBarItems method, which has two arguments, leading and trailing, to add AnyView to the left and right of the title bar, respectively.

Text("Hello, World!" NavigationBarItems (leading: Button(" 1") {self.score -= 1}, trailing: Text(" button 2")Copy the code

You can also add multiple different views to the same View

Text("Hello, World!" ). NavigationBarTitle (" I am the navigationBarTitle "). NavigationBarItems (leading: HStack {self.score -= 1} HStack {self.score -= 1} HStack {self.score -= 1}Copy the code