SwiftUI -当NavigationStack的根视图中没有标题或标题为空时,NavigationBar在Push时



我遇到了一个问题,在SwiftUI的导航栏中,当细节视图被推送时,它不会动画,并且在NavigationStack的根视图中没有或空[navigation]标题。

下面是一个再现这个问题的例子:

//
//  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()
}
}

编辑:

iPhone 11 Pro (iOS 16.4)和iPhone 12 (iOS 16.1.1)测试

编辑2:

NavigationView工作正常

//
//  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()
}
}

下面的代码解决了我的问题

//
//  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()
@State private var isNavigationBarHidden: Bool = true
var body: some View {
NavigationStack(path: $navigationPath) {
Button("Go to Details View") {
navigationPath.append(NavigationRoute.details)
}
// >>> EMPTY TITLE BUT ANIMATION WORKS
.navigationTitle("") // this sets the Back button text when a new screen is pushed
.navigationBarTitleDisplayMode(.inline)
.toolbar {
ToolbarItem(placement: .principal) {
Text("") // this sets the screen title in the navigation bar, when the screen is visible
}
}
.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")
.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()
}
}

相关内容

  • 没有找到相关文章

最新更新