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

In SwiftUI with attached code when i set navigationBarBackButtonHidden to true, the NavigationStack

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") // 

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744109952a4558905.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信