NEW: Get your ticket for Hacking with Swift Live 2019! >>

< Working with containers   How to add bar items to a navigation view >

How to embed a view in a navigation view

SwiftUI’s NavigationView maps more or less to UIKit’s UINavigationController in that it presents content, it’s able to handle navigation between views, and it places a navigation bar at the top of the screen.

In its simplest form you can place a text view into a navigation view like this:

NavigationView {
    Text("This is a great app")
}

However, that leaves the navigation bar at the top empty. So, you will usually use the navigationBarTitle() modifier on whatever you’re embedding, so you can add a title at the top of your screen, like this:

NavigationView {
    Text("SwiftUI")
        .navigationBarTitle(Text("Welcome"))
}

The navigationBarTitle() modifier gives us some customization options. For example, by default it will inherit large title display mode from whatever view presented it, or if it’s the initial view then it will use large titles. But if you’d prefer to force enable or disable large titles you should use the inline parameter like this:

.navigationBarTitle(Text("Welcome"), displayMode: .inline)

That will make small navigation titles, but you can also use .large to force a large title.

HACKING WITH SWIFT LIVE This July is a new two-day event where you'll be inspired by great speakers on day one then learn all the amazing new features from WWDC on day two – click here for more information and tickets.

< Working with containers   How to add bar items to a navigation view >
MASTER SWIFT NOW
Buy Testing Swift Buy Practical iOS 12 Buy Pro Swift Buy Swift Design Patterns Buy Swift Coding Challenges Buy Server-Side Swift (Vapor Edition) Buy Server-Side Swift (Kitura Edition) Buy Hacking with macOS Buy Advanced iOS Volume One Buy Advanced iOS Volume Two Buy Hacking with watchOS Buy Hacking with tvOS Buy Hacking with Swift Buy Dive Into SpriteKit Buy Swift in Sixty Seconds Buy Objective-C for Swift Developers Buy Beyond Code

Was this page useful? Let me know!

Click here to visit the Hacking with Swift store >>