如何在 SwiftUI 中关闭或切换到另一个应用程序时保持 NavigationLink 处于活动状态

问题描述 投票:0回答:1

我在导航到详细信息页面时遇到问题,但是当我尝试打开另一个应用程序或关闭该应用程序时,最初处于活动状态的导航链接突然自行关闭。而过去2021年到2023年初没有这样的问题,我对iOS版本更新持怀疑态度。

我为目标应用程序设置了最低 iOS 14 版本。但从我拥有的整个复杂代码来看,这里我只分享我当前使用的示例代码。 我使用以下代码:

import SwiftUI

struct ContentView: View {
  init() {}
  
  var body: some View {
    NavigationView {
      TabView {
        VStack {
          NavigationLink {
            Text(“Detail Home Page”)
              .foregroundColor(.blue)
          } label: {
            Image(systemName: “globe”)
              .foregroundColor(.red)
            Text(“Home Page”)
              .foregroundColor(.red)
          }
        }
        .tabItem {
          Label(“HOME”, image: “home”)
        }
      }
      .accentColor(.white)
    }
  }
}

enter image description here

我知道还有另一种方法可以使用下面的代码来解决此问题,但是 TabBar/TabView 在导航时不会消失。

TabView {
  NavigationView {
    VStack {
      NavigationLink {
        Text(“Detail Home Page”)
          .foregroundColor(.blue)
      } label: {
        Image(systemName: “globe”)
          .foregroundColor(.red)
        Text(“Home Page”)
          .foregroundColor(.red)
      }
    }
  }
  .tabItem {
    Label(“HOME”, image: “home”)
  }
}
.accentColor(.white)

enter image description here

您对我有什么建议吗?请帮忙

swiftui ios14 swiftui-navigationview swiftui-tabview
1个回答
0
投票

您的结构存在问题:

TabView
应始终位于视图层次结构的顶部。由于
NavigationView
已被弃用,您还应该考虑使用
NavigationStack
。一般来说,会是:

TabView {
  NavigationStack {
    VStack {
       ...
    }
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.