Adding buttons to NavigationView and activating navigation programmatically
As in UIKit, it is possible to add custom buttons to NavigationView
. This is achieved by using the .toolbar
modifier and placing a ToolbarItem
inside it.
If you don’t specify the placement, for left-to-right languages, the default placement of .ToolbarItem
will be positioned on the right, and it will switch to the left for right-to-left languages.
Typically, you will add a Button
to ToolbarItem
. But if you are on iOS 16, NavigationView
can be substituted with NavigationStack
, without changing anything else.
The following code example illustrates how to add buttons to a NavigationView
, specifying the placement explicitly in both cases:
import SwiftUI struct ContentView: View { var body: some View { NavigationView { Text("Example of adding tabbaritems...