使用 NavigationLink isActive 时视图之间的动画不起作用

fut*_*dam 5 animation swiftui swiftui-navigationlink

我从 UIKit 转到 SwiftUI,但在呈现新视图时遇到了 NavigationLink 没有动画的问题。

当以下属性非零时,我已设置视图结构以包含 NavigationLink:

@State private var retrievedDeviceIdentity: Proteus.DeviceIdentity?
Run Code Online (Sandbox Code Playgroud)

Proteus.DeviceIdentity 类型是一个基本数据结构。该属性由成功的异步闭包填充,而不是直接的用户交互。因此,视图结构的设置如下所示,使用 NavigationLink 的destination:isActive:label:初始化程序:

var body: some View {
    NavigationView {
        VStack {
            Form {
                // Form building
            }
            
            if let deviceIdentity = retrievedDeviceIdentity {
                NavigationLink(
                    destination: AddDeviceLinkDeviceForm(deviceIdentity: deviceIdentity),
                    isActive: .constant(retrievedDeviceIdentity != nil),
                    label: {
                        EmptyView()
                    }
                )
                .onDisappear() {
                    updateSyncButtonEnabledState()
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

retrievedDeviceIdentity填充为非零时,新视图确实会呈现。但是,没有幻灯片过渡到该视图;它会立即改变。在该新视图中时,点击后退按钮确实会幻灯片切换回该视图。

任何想法如何解决这一问题?由于我对 SwiftUI 还很陌生,如果我设置了错误的新结构,那么我也欢迎对此提供反馈。

(我在 macOS Big Sur 11.0.1 上使用 Xcode 12.3。)

fut*_*dam 5

@Asperi 已经很接近了,但是移动 NavigationLink 导致视图根本不显示。

所做工作是将if支架拆开retrievedDeviceIdentity

var body: some View {
NavigationView {
    VStack {
        Form {
            // Form building
        }
        
        NavigationLink(
            destination: AddDeviceLinkDeviceForm(deviceIdentity: deviceIdentity),
            isActive: .constant(retrievedDeviceIdentity != nil),
            label: {
                EmptyView()
            }
        )
        .onDisappear() {
            updateSyncButtonEnabledState()
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这需要将 AddDeviceLinkDeviceForm 的deviceIdentity属性设为可选以接受包装的值。