最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

ios - NavigationStack inconsistent push animation with zoom navigation transition when back button hidden - Stack Overflow

programmeradmin8浏览0评论

In SwiftUI with attached code when i set navigationBarBackButtonHidden to true, the NavigationStack's navigation bar always pushes in when I dismiss the detail view. Since it's a zoom transition, I want the navigation bar to stay and not animate. How do i solve this?

import SwiftUI

struct ContentView: View {
    @Namespace private var namespace
    var body: some View {
        NavigationStack {
            NavigationLink {
                DetailView()
                    // here is the issue: if I set it to true, it will have that default push animation when dismissed, which is inconsistent with the zoom animation.
                    .navigationBarBackButtonHidden(false)
                    .navigationTransition(.zoom(sourceID: "world", in: namespace))
            } label: {
                Image(systemName: "globe")
                    .matchedTransitionSource(id: "world", in: namespace)
            }
            .navigationTitle("Main")
            .navigationBarTitleDisplayMode(.inline)
            .toolbar {
                ToolbarItem(placement: .primaryAction){
                    Button {
                        print("noop")
                    } label: {
                        Label("Toggle", systemImage: "rectangle.grid.1x2")
                    }
                }
            }
        }
    }
}

struct DetailView: View {
    var body: some View {
        ZStack{
            Color.indigo
            Text("body here")
        }.ignoresSafeArea()
    }
}

#Preview {
    ContentView()
}

In SwiftUI with attached code when i set navigationBarBackButtonHidden to true, the NavigationStack's navigation bar always pushes in when I dismiss the detail view. Since it's a zoom transition, I want the navigation bar to stay and not animate. How do i solve this?

import SwiftUI

struct ContentView: View {
    @Namespace private var namespace
    var body: some View {
        NavigationStack {
            NavigationLink {
                DetailView()
                    // here is the issue: if I set it to true, it will have that default push animation when dismissed, which is inconsistent with the zoom animation.
                    .navigationBarBackButtonHidden(false)
                    .navigationTransition(.zoom(sourceID: "world", in: namespace))
            } label: {
                Image(systemName: "globe")
                    .matchedTransitionSource(id: "world", in: namespace)
            }
            .navigationTitle("Main")
            .navigationBarTitleDisplayMode(.inline)
            .toolbar {
                ToolbarItem(placement: .primaryAction){
                    Button {
                        print("noop")
                    } label: {
                        Label("Toggle", systemImage: "rectangle.grid.1x2")
                    }
                }
            }
        }
    }
}

struct DetailView: View {
    var body: some View {
        ZStack{
            Color.indigo
            Text("body here")
        }.ignoresSafeArea()
    }
}

#Preview {
    ContentView()
}

Share Improve this question edited Mar 27 at 15:04 Benzy Neez 23.3k3 gold badges15 silver badges44 bronze badges asked Mar 27 at 5:57 randomorrandomor 5,6855 gold badges49 silver badges70 bronze badges 1
  • Is the behavior the same when you drag to dismiss the body here view? – sonle Commented Mar 27 at 6:41
Add a comment  | 

2 Answers 2

Reset to default 1

The animation seems to be associated with the navigation bar being removed, because there is no content.

The workaround you showed in your answer is retaining some content, so the navigation bar is not removed.

You could also try setting a navigation title:

DetailView()
    .navigationBarBackButtonHidden(true)
    .navigationTransition(.zoom(sourceID: "world", in: namespace))
    .navigationTitle("Detail") // 

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论