swift - SwiftUI:导航到视图层次结构中的视图
问题描述
到目前为止我所拥有的:
我有两个视图,第一个 myContentView
将加载到scene(_:willConnectTo:options:)
方法中SceneDelegate
(这是标准的)。我已将 a 嵌入NavigationView
到我的ContentView
. 第二个视图DetailView
可以通过 导航到NavigationLink
,要导航回来,您可以使用NavigationView
.
struct ContentView: View {
var body: some View {
NavigationView {
Form {
NavigationLink(destination: DetailView()) {
Text("Navigate")
}
}
.navigationBarTitle("Some Title")
}
}
}
struct DetailView: View {
var body: some View {
Text("Detail")
.navigationBarTitle("Another Title")
}
}
内部scene(_:willConnectTo:options:)
方法SceneDelegate
:
let contentView = ContentView()
if let windowScene = scene as? UIWindowScene {
let window = UIWindow(windowScene: windowScene)
window.rootViewController = UIHostingController(rootView: contentView)
self.window = window
window.makeKeyAndVisible()
}
我正在尝试做的事情:
现在我想DetailView
根据connectionOptions
.scene(_:willConnectTo:options:)
SceneDelegate
我的问题:
当我只是根据替换let contentView = ContentView()
时,我不再在我的视图层次结构中,因此我没有布局。let contentView = DetailView
connectionOptions
ContentView
NavigationView
问题:
如何导航到视图层次结构中的某个视图,而不会丢失由NavigationView
另一个视图内部创建的任何导航功能(包括导航栏、后退按钮以及能够更深入地导航)?
解决方案
使用NavigationLink(_,destination:,tag:,selection:)
并绑定selection
到来自connectionOptions
.
有关详细说明和示例代码,请参阅https://nalexn.github.io/swiftui-deep-linking/。
推荐阅读
- postgresql - jsonb 字段之间的 CriteriaBuilder
- c++ - 如何实现零拷贝形式的gRPC c++
- html - 如何编写仅适用于移动设备而不适用于具有相同宽度的笔记本电脑的 CSS
- javascript - 无法使用 Vue3-leaflet 访问 Leaflet 对象
- android - 无法为 PopupMenu 设置 TextColor
- javascript - 以编程方式更改 Microsoft 管理控制台的 mmc 视图的结果视图模式?
- javascript - 隐藏包装div后iframe内容消失
- ios - Apple Silicon 上的导入框架
- powershell - 如何创建 Azure Monitor - 使用 Azure .Net SDK 或 Powershell 脚本的虚拟机警报
- delphi - 如果 Wrapmode 设置为“Fit”,则在 TImage 中获取位图的新宽度和高度