NEW: Learn SwiftUI with my free YouTube video series! >>

< How to embed a view in a navigation view   How to embed views in a tab bar using TabView >

How to add bar items to a navigation view

The navigationBarItems() modifier lets us add bar button items to the leading and trailing edge of a navigation view. These might be a tappable button, but there are no restrictions – you can add any sort of view.

For example, this adds a Help button the to trailing edge of a navigation view:

var body: some View {
    NavigationView {
        Text("SwiftUI")
            .navigationBarTitle("Welcome")
            .navigationBarItems(trailing:
                Button(action: {
                    print("Help tapped!")
                }) {
                    Text("Help")
                })
    }
}

LEARN SWIFTUI FOR FREE I wrote a massive, free SwiftUI tutorial collection, and also have a growing list of free SwiftUI tutorials on YouTube – get started today!

< How to embed a view in a navigation view   How to embed views in a tab bar using TabView >
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!

Average rating: 2.6/5

Click here to visit the Hacking with Swift store >>