How to disable NavigationView push and pop animations

前端 未结 2 453
醉酒成梦
醉酒成梦 2021-01-01 17:25

Given this simple NavigationView:

struct ContentView : View {
    var body: some View {
        Naviga         


        
相关标签:
2条回答
  • 2021-01-01 17:47

    I recently created an open source project called swiftui-navigation-stack (https://github.com/biobeats/swiftui-navigation-stack) that contains the NavigationStackView, a view that mimics the navigation behaviours of the standard NavigationView adding some useful features. For example, you could use the NavigationStackView and disable the transition animations as requested by Kontiki in the question. When you create the NavigationStackView just specify .none as transitionType:

    struct ContentView : View {
        var body: some View {
            NavigationStackView(transitionType: .none) {
                ZStack {
                    Color.yellow.edgesIgnoringSafeArea(.all)
    
                    PushView(destination: View2()) {
                        Text("PUSH")
                    }
                }
            }
        }
    }
    
    struct View2: View {
        var body: some View {
            ZStack {
                Color.green.edgesIgnoringSafeArea(.all)
                PopView {
                    Text("POP")
                }
            }
        }
    }
    

    PushView and PopView are two views that allow you push and pop views (similar to the SwiftUI NavigationLink). Here is the complete example:

    import SwiftUI
    import NavigationStack
    
    struct ContentView : View {
        var body: some View {
            NavigationStackView(transitionType: .none) {
                ZStack {
                    Color.yellow.edgesIgnoringSafeArea(.all)
    
                    PushView(destination: View2()) {
                        Text("PUSH")
                    }
                }
            }
        }
    }
    
    struct View2: View {
        var body: some View {
            ZStack {
                Color.green.edgesIgnoringSafeArea(.all)
                PopView {
                    Text("POP")
                }
            }
        }
    }
    
    struct ContentView_Previews: PreviewProvider {
        static var previews: some View {
            ContentView()
        }
    }
    

    The result is:

    It would be great if you guys joined me in improving this open source project.

    0 讨论(0)
  • 2021-01-01 17:54

    Xcode 11.3:

    Right now there is no modifier to disable NavigationView animations.

    You can use your struct init() to disable animations, as below:

    struct ContentView : View {
    
        init(){
            UINavigationBar.setAnimationsEnabled(false)
        }
    
        var body: some View {
            NavigationView {
                VStack {
                    NavigationLink("Push Me", destination: Text("PUSHED VIEW"))
                }
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题