gpt4 book ai didi

swiftui - 在 Swift UI 中导致灰屏的导航链接

转载 作者:行者123 更新时间:2023-12-04 03:39:27 25 4
gpt4 key购买 nike

我是 SwiftUI 的新手,我有一个错误,当我使用太多导航链接时,我的整个屏幕都会变灰。在研究错误时我找不到任何解决方案。我正在最新版本的 Xcode 12.4 上运行该项目。我当前的设置是有 2 个不同的 swiftUI View ,每个 View 都包含一个指向另一个的导航链接。

This is what it looks like

代码:

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)
}
}

Project file

最佳答案

View 层次结构中应该只有一个 NavigationView

尝试在根级别创建一个 NavigationView:

struct ContentView: View {
var body: some View {
NavigationView {
PageOne()
.navigationBarHidden(true)
.navigationBarBackButtonHidden(true)
}
}
}

然后从 subview 中删除 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)
}
}

关于swiftui - 在 Swift UI 中导致灰屏的导航链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66324172/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com