I'm experiencing an issue with the NavigationBar in SwiftUI where it doesn't animate when Detail View is pushed and there is no or empty [navigation] title in the root view of a NavigationStack
.
Here is an example that reproduces the issue:
//
// ContentView.swift
// iTestNavStackFresh
//
// Created by Aleksandar Petrov on 6.04.23.
//
import SwiftUI
enum NavigationRoute {
case details
}
struct ContentView: View {
@State private var navigationPath = NavigationPath()
var body: some View {
NavigationStack(path: $navigationPath) {
Button("Go to Details View") {
navigationPath.append(NavigationRoute.details)
}
// .navigationTitle("ANY NON EMPTY TITLE WILL DO") // <-- Uncomment to fix the issue
// .navigationBarTitleDisplayMode(.inline)
.navigationDestination(for: NavigationRoute.self) { navigationRoute in
switch navigationRoute {
case .details:
DetailView()
.toolbarBackground(Color.white, for: .navigationBar)
.toolbarBackground(.visible, for: .navigationBar)
}
}
}
}
}
struct DetailView: View {
var body: some View {
Text("Detail View")
.font(.title)
.frame(maxWidth: .infinity, maxHeight: .infinity)
.background(
Color.black
.opacity(0.3)
)
.navigationTitle("Detail View")
.navigationBarTitleDisplayMode(.inline)
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
Edit:
Tested on iPhone 11 Pro (iOS 16.4) and iPhone 12 (iOS 16.1.1)
Edit 2:
NavigationView
works as expected
//
// ContentView.swift
// iTestNavStackFresh
//
// Created by Aleksandar Petrov on 6.04.23.
//
import SwiftUI
struct ContentView: View {
var body: some View {
NavigationView {
NavigationLink(
destination: DetailView()
.toolbarBackground(Color.white, for: .navigationBar)
.toolbarBackground(.visible, for: .navigationBar)
) {
Text("Hello, World!")
}
}
}
}
struct DetailView: View {
var body: some View {
Text("Detail View")
.navigationTitle("Detail View")
.navigationBarTitleDisplayMode(.inline)
.font(.title)
.frame(maxWidth: .infinity, maxHeight: .infinity)
.background(
Color.black
.opacity(0.3)
)
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}