如何禁用NavigationStack推动动画

发布于 2025-02-12 18:53:19 字数 883 浏览 1 评论 0原文

我希望立即出现的视图。而不是默认的iOS推动动画。

此方法使用NavigationView + navigationLink一起使用。 iOS 16弃用navigationView

enum Nav {
    case a
}

struct ContentView: View {
    @State var nav: [Nav] = []
    
    var body: some View {
        NavigationStack(path: $nav) {
            Button {
                var transaction = Transaction()
                transaction.disablesAnimations = true
                withTransaction(transaction) {
                    nav = [.a] // Doesn't work. Still animates
                }
            } label: {
                Text("Go to a")
            }
                .navigationDestination(for: Nav.self) { path in
                    switch path {
                    case .a:
                        Text("a")
                    }
                }
        }
    }
}

I'd like the view to instantly appear. Instead of the default iOS push animation.

This method USED to work with a NavigationView + NavigationLink. iOS 16 deprecated NavigationView

enum Nav {
    case a
}

struct ContentView: View {
    @State var nav: [Nav] = []
    
    var body: some View {
        NavigationStack(path: $nav) {
            Button {
                var transaction = Transaction()
                transaction.disablesAnimations = true
                withTransaction(transaction) {
                    nav = [.a] // Doesn't work. Still animates
                }
            } label: {
                Text("Go to a")
            }
                .navigationDestination(for: Nav.self) { path in
                    switch path {
                    case .a:
                        Text("a")
                    }
                }
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

羁客 2025-02-19 18:53:19

这看起来像是一个错误,值得向苹果提交反馈。

同时,这是可能的解决方法。用Xcode 14B2 / iOS测试16

Button {
    UIView.setAnimationsEnabled(false)

        nav = [.a]

    DispatchQueue.main.asyncAfter(deadline: .now() + 0.25) {
        UIView.setAnimationsEnabled(true)
    }
} label: {
  // ...

This looks like a bug and worth submitting feedback to Apple.

Meanwhile here is a possible workaround. Tested with Xcode 14b2 / iOS 16

Button {
    UIView.setAnimationsEnabled(false)

        nav = [.a]

    DispatchQueue.main.asyncAfter(deadline: .now() + 0.25) {
        UIView.setAnimationsEnabled(true)
    }
} label: {
  // ...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文