You need to enable JavaScript to run this app. iOS 14.5下withAnimation执行时点击NavigationLink致EnvironmentObject为nil问题
优惠活动
大模型
产品
解决方案
定价
更多

iOS 14.5下withAnimation执行时点击NavigationLink致EnvironmentObject为nil问题

问题根因

iOS 14.5 对NavigationView的动画处理和状态重置逻辑做了调整,当withAnimation代码块执行过程中触发NavigationLink跳转时,挂载在NavigationView外层的EnvironmentObject会在导航栈重新渲染时丢失,导致子视图找不到对应的环境对象崩溃。

可行解决方案

方案1:给NavigationLink的目标视图手动注入环境对象

这是兼容性最高的修复方案,直接给每个跳转的目标视图显式传入EnvironmentObject,避免系统导航栈渲染时丢失环境变量:
修改对应NavigationLink的destination参数,手动追加.environmentObject(backgroundVM)即可:

// ContentView中的NavigationLink修改为
NavigationLink(
    destination: View2().environmentObject(backgroundVM),
    isActive: $presentNext) {
    Button {
        presentNext = true
    } label: {
        Text("Navigate")
    }
}

// View2中的NavigationLink修改为
NavigationLink(
    destination: View3().environmentObject(backgroundVM),
    isActive: $presentNext) {
    Button {
        presentNext = true
    } label: {
        Text("Navigate")
    }
}

方案2:跳转时临时禁用动画避免状态冲突

如果不需要导航跳转动画,可以在触发跳转时禁用动画,避开和正在执行的背景动画逻辑冲突:
修改按钮点击逻辑,用withAnimation(nil)包裹状态赋值:

// ContentView中的导航按钮点击逻辑修改为
Button {
    withAnimation(nil) {
        presentNext = true
    }
} label: {
    Text("Navigate")
}

// View2中的导航按钮点击逻辑同步修改即可
完整修复后示例代码(方案1实现)
import SwiftUI
import Combine

struct ContentView: View {
    @StateObject var backgroundVM = BackgroundViewModel()
    @State var presentNext: Bool = false
    
    var body: some View {
        NavigationView {
            VStack{
                Text("Hello, world!")
                    .padding()
                NavigationLink(
                    destination: View2().environmentObject(backgroundVM),
                    isActive: $presentNext) {
                    Button {
                        presentNext = true
                    } label: {
                        Text("Navigate")
                    }
                }
            }
            .frame(width: 250, height: 250, alignment: .center)
            .background(backgroundVM.backgroundColor)
        }
        .environmentObject(backgroundVM)
        .onReceive(backgroundVM.timer) { _ in
            withAnimation(.easeInOut(duration: 2.0)) {
                backgroundVM.setColor()
            }
        }
    }
}

struct View2: View {
    @EnvironmentObject var backgroundVM: BackgroundViewModel
   
    @State var presentNext: Bool = false
    
    var body: some View {
        VStack {
            Text("View 2")
            NavigationLink(
                destination: View3().environmentObject(backgroundVM),
                isActive: $presentNext) {
                Button {
                    presentNext = true
                    
                } label: {
                    Text("Navigate")
                }
            }
        }
        .frame(width: 250, height: 250, alignment: .center)
        .background(backgroundVM.backgroundColor)
    }
}

struct View3: View {
    @EnvironmentObject var backgroundVM: BackgroundViewModel
    
    var body: some View {
        VStack {
            Text("View 3")
        }
        .frame(width: 250, height: 250, alignment: .center)
        .background(backgroundVM.backgroundColor)
 
    }
}

class BackgroundViewModel: ObservableObject {
    @Published var backgroundColor: Color = .orange
    @Published var colorIndex: Int = 0 {
        willSet {
            backgroundColor = colors[newValue]
        }
    }
    
    var timer = Timer.publish(every: 1.5, on: .main, in: .common)
        .autoconnect()
    
    var colors: [Color] = [.orange, .green, .purple]
    
    func setColor() {
        if colorIndex + 1 == colors.count {
            colorIndex = 0
        } else {
            colorIndex += 1
        }
    }
}

内容的提问来源于stack exchange,提问作者NFarrell

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.21 05:45:00