Why this happens
SwiftUI’s NavigationLink expects to be the direct tap target. When you place a Button inside a NavigationLink, the button’s action handler consumes the tap gesture, preventing the link from activating. Additionally, NavigationView (deprecated in iOS 16) only shows a navigation bar when it contains a NavigationLink or when .navigationTitle is applied to a child view inside the navigation hierarchy—not on the NavigationView itself.
The original code also uses the deprecated Edge.Set syntax (.padding(Edge.Set.bottom, 50)) which should be .padding(.bottom, 50).
How to fix it
1. Modern approach: NavigationStack with NavigationPath (iOS 16+)
This is the recommended pattern. You hold a NavigationPath in state, append values to it when navigation should occur, and declare destinations with .navigationDestination(for:).
import SwiftUI
struct LoginView: View {
@State private var email = ""
@State private var password = ""
@State private var path = NavigationPath()
var body: some View {
NavigationStack(path: $path) {
VStack(alignment: .leading, spacing: 16) {
Text("Let's get you signed in.")
.bold()
.font(.system(size: 40))
.frame(width: 300, height: 100, alignment: .topLeading)
.padding(.bottom, 50)
Text("Email address:")
.font(.headline)
TextField("Email", text: $email)
.frame(height: 44)
.textFieldStyle(.roundedBorder)
Text("Password:")
.font(.headline)
SecureField("Password", text: $password)
.frame(height: 44)
.textFieldStyle(.roundedBorder)
Button {
// Perform authentication logic here
path.append("DetailView")
} label: {
Text("Login")
.frame(maxWidth: .infinity)
.padding()
.background(Color.gray)
.foregroundColor(.white)
.cornerRadius(8)
}
.padding(.vertical, 20)
}
.padding(.horizontal, 30)
.navigationTitle("Login")
.navigationDestination(for: String.self) { value in
if value == "DetailView" {
DetailView()
}
}
}
}
}
struct DetailView: View {
var body: some View {
Text("Detail View")
.navigationTitle("Detail")
}
}
Key points:
– NavigationStack(path: $path) binds the stack to your mutable path.
– path.append("DetailView") pushes programmatically from anywhere (button action, async callback, etc.).
– .navigationDestination(for: String.self) maps each hashable value to a destination view.
2. Legacy approach: NavigationLink with tag/selection (iOS 13–15)
If you must support iOS 15 or earlier, use a hidden NavigationLink driven by a @State selection binding.
struct LoginViewLegacy: View {
@State private var email = ""
@State private var password = ""
@State private var selection: Int? = nil
var body: some View {
NavigationView {
VStack(alignment: .leading, spacing: 16) {
Text("Let's get you signed in.")
.bold()
.font(.system(size: 40))
.frame(width: 300, height: 100, alignment: .topLeading)
.padding(.bottom, 50)
Text("Email address:")
.font(.headline)
TextField("Email", text: $email)
.frame(height: 44)
.textFieldStyle(.roundedBorder)
Text("Password:")
.font(.headline)
SecureField("Password", text: $password)
.frame(height: 44)
.textFieldStyle(.roundedBorder)
NavigationLink(destination: DetailView(), tag: 1, selection: $selection) {
Button {
selection = 1
} label: {
Text("Login")
.frame(maxWidth: .infinity)
.padding()
.background(Color.gray)
.foregroundColor(.white)
.cornerRadius(8)
}
}
.padding(.vertical, 20)
}
.padding(.horizontal, 30)
.navigationTitle("Login")
}
}
}
Notes:
– The NavigationLink wraps the Button but has no visible label of its own; the button becomes the trigger.
– When selection matches the tag (1), the link activates.
– .navigationTitle is placed on the content inside NavigationView, not on the NavigationView itself.
3. Simpler legacy variant: isActive binding
For a single link you can use a Bool instead of an Int? tag.
@State private var showDetail = false
NavigationLink(destination: DetailView(), isActive: $showDetail) {
Button("Login") { showDetail = true }
}
How to prevent it
- Use
NavigationStack+NavigationPathfor all new projects targeting iOS 16+. - Keep navigation triggers outside of button actions when using the old
NavigationLinkAPI—wrap the button in the link, not the other way around. - Apply
.navigationTitleto the child view inside the navigation container, not to the container itself. - Migrate deprecated
Edge.Setpadding to modern edge insets (.padding(.bottom, 50)). - Test navigation on both iPhone and iPad;
NavigationStackadapts automatically.
FAQ
Why does my navigation bar not appear with NavigationView?
NavigationView only shows a bar when a child view provides a title via .navigationTitle. In the question the title was applied to the NavigationView itself (.navigationBarTitle(Text("Login")) on the outside), which has no effect. Move .navigationTitle("Login") onto the VStack or the root view inside NavigationView.
Can I navigate after an async login call?
Yes. With NavigationPath simply await your authentication, then path.append("DetailView") on the main thread. The legacy selection binding works the same way—set the state variable after the async work completes.
Is NavigationView completely removed in iOS 17?
NavigationView is deprecated but still available. Apple recommends NavigationStack for hierarchical navigation and NavigationSplitView for sidebar-style layouts. New code should use the modern APIs.
Parts of this guide are adapted from content licensed under CC BY-SA 4.0; this article is shared under the same license.