swiftui - 导航链接导致 Swift UI 中出现灰屏
问题描述
我是 SwiftUI 的新手,遇到一个错误,当我使用太多导航链接时,我的整个屏幕变灰。在研究错误时我找不到任何解决方案。我正在最新版本的 Xcode 12.4 上运行该项目。我目前的设置是有 2 个不同的 swiftUI 视图,每个视图都包含一个导航链接到另一个。
代码:
PageOne.swift
struct PageOne: View {
var body: some View {
NavigationView {
VStack {
Text("This is page 1")
.font(.system(size: 36, weight: .bold))
.padding(.bottom)
NavigationLink(
destination: PageTwo(),
label: {
VStack {
Text("Go to Page 2")
.font(.system(size: 24, weight: .medium))
.foregroundColor(.white)
.frame(width: 200, height: 50, alignment: .center)
.background(Color.blue)
.cornerRadius(12)
}
})
}
}
.navigationBarHidden(true)
.navigationBarBackButtonHidden(true)
}
}
PageTwo.swift
struct PageTwo: View {
var body: some View {
NavigationView {
VStack {
Text("This is page 2")
.font(.system(size: 36, weight: .bold))
.padding(.bottom)
NavigationLink(
destination: PageOne(),
label: {
VStack {
Text("Go to Page 1")
.font(.system(size: 24, weight: .medium))
.foregroundColor(.white)
.frame(width: 200, height: 50, alignment: .center)
.background(Color.blue)
.cornerRadius(12)
}
})
}
}
.navigationBarHidden(true)
.navigationBarBackButtonHidden(true)
}
}
解决方案
您应该在视图层次结构中只有一个NavigationView
。
尝试NavigationView
在根级别创建一个:
struct ContentView: View {
var body: some View {
NavigationView {
PageOne()
.navigationBarHidden(true)
.navigationBarBackButtonHidden(true)
}
}
}
然后NavigationView
从子视图中删除:
struct PageOne: View {
var body: some View {
VStack {
Text("This is page 1")
.font(.system(size: 36, weight: .bold))
.padding(.bottom)
NavigationLink(
destination: PageTwo(),
label: {
VStack {
Text("Go to Page 2")
.font(.system(size: 24, weight: .medium))
.foregroundColor(.white)
.frame(width: 200, height: 50, alignment: .center)
.background(Color.blue)
.cornerRadius(12)
}
})
}
.navigationBarHidden(true)
.navigationBarBackButtonHidden(true)
}
}
struct PageTwo: View {
var body: some View {
VStack {
Text("This is page 2")
.font(.system(size: 36, weight: .bold))
.padding(.bottom)
NavigationLink(
destination: PageOne(),
label: {
VStack {
Text("Go to Page 1")
.font(.system(size: 24, weight: .medium))
.foregroundColor(.white)
.frame(width: 200, height: 50, alignment: .center)
.background(Color.blue)
.cornerRadius(12)
}
})
}
.navigationBarHidden(true)
.navigationBarBackButtonHidden(true)
}
}
推荐阅读
- python - 使用 python 从网站的 URL 创建数据框
- javascript - Chrome 中的解析错误
- angularjs - 通过 Google Compute Shell 部署 Spring Boot 服务和 UI 后出现 CORS 错误
- c - 为什么我必须减少 putc 函数中的cnt?我还有一个角色。增加它不符合逻辑吗?
- python - 简化尝试,除了 yahooquery
- c# - 如何以编程方式获取 NotifyIcon.Icon 属性的值?
- java - 为什么服务器在非多线程的情况下会同时处理多个客户端?
- python - 计算文件中的行数时如何避免“未使用的变量”警告?
- c# - 修复自定义响应“.NET Core API Gateway Ocelot - Middleware”的问题
- c# - Angular 8 - 覆盖内容长度