NavigationView in SwiftUI is a container view which allows you to manage other views in a navigation interface. Learn how to customize navigation bar with a title (large or small), add leading and trailing buttons to the navigation bar, and implement a master-detail flow where you can push detail view on top of the master view.
Create NavigationView
Convert your view into NavigationView like that:
struct ContentView: View {
var body: some View {
NavigationView {
Text("SwiftUI tutorials")
}
}
}
Add a title to the NavigationView
Use the navigationBarTitle() modifier inside of the NavigationView to create a navigation bar with a title:
NavigationView {
Text("SwiftUI tutorials")
.navigationBarTitle("Master view")
}
Customize the navigation bar title
By default, the navigation bar title uses a .large display mode, which is presented in the screenshot above.
Change the title display mode of the navigation bar to .inline to make the title small:
NavigationView {
Text("SwiftUI tutorials")
.navigationBarTitle("Master view", displayMode: .inline)
}
Add buttons to the navigation view
Add leading and trailing buttons to the navigation bar using navigationBarItems() modifier. Leading button is an SF Symbol, trailing button is text:
NavigationView {
Text("SwiftUI tutorials")
.navigationBarTitle("Master view")
.navigationBarItems(leading:
Button(action: {
print("SF Symbol button pressed...")
}) {
Image(systemName: "calendar.circle").imageScale(.large)
},
trailing:
Button(action: {
print("Edit button pressed...")
}) {
Text("Edit")
}
)
}
You can add multiple buttons to the leading or trailing bar items by using an HStack:
NavigationView {
Text("SwiftUI tutorials")
.navigationBarTitle("Master view")
.navigationBarItems(trailing:
HStack {
Button(action: {
print("SF Symbol button pressed...")
}) {
Image(systemName: "calendar.circle")
.imageScale(.large)
}
Button(action: {
print("Edit button pressed...")
}) {
Text("Edit")
}
}
)
}
How to push new view onto NavigationView
In order to implement the master-detail flow and present a detail view on top of the master view in NavigationView use NavigationLink – a button that triggers a navigation presentation when pressed:
struct DetailView: View {
var body: some View {
Text("Detail view")
}
}
struct ContentView: View {
var body: some View {
NavigationView {
NavigationLink(destination: DetailView()) {
Text("Show detail view")
}
.navigationBarTitle("Master view")
}
}
}
The NavigationView automatically handles adding the back button when detail view is presented.
If you’d like to add a title to the detail view, use the navigationBarTitle() modifier inside of your DetailView body:
struct DetailView: View {
var body: some View {
Text("Detail view")
.navigationBarTitle("Detail view", displayMode: .inline)
}
}
Related tutorials:
- How to present new sheet view from navigationBarItems button in SwiftUI
- How to add button to navigation bar in SwiftUI
- How to add NavigationView to List in SwiftUI and show detail view using NavigationLink
Check out Apple’s official documentation of NavigationView.