SwiftUI NavigationView 显示不正确
SwiftUI NavigationView displaying incorrectly
我正在学习 SwiftUI 并遵循 Apple 的 'Landmark' 教程,然后学习示例并尝试使用这些知识制作自己的作品。
但是,我确实缺少一些理解,因为我不明白为什么我会出现 non-standard 行为。
苹果代码:
struct CategoryHome: View
{
var body: some View
{
NavigationView
{
Text("Landmarks Content")
.navigationBarTitle(Text("Featured"))
}
}
}
它产生这个:
我的代码:
struct ContentView: View
{
@State private var selection: String? = nil
var body: some View
{
NavigationView
{
NavigationLink(destination: Text("Second View"), tag: "Second", selection: $selection) { EmptyView() }
NavigationLink(destination: Text("Third View"), tag: "Third", selection: $selection) { EmptyView() }
NavigationLink(destination: Text("Forth View"), tag: "Forth", selection: $selection) { EmptyView() }
VStack
{
Text("Content View")
Spacer()
Button("Press to goto 2nd view", action: { self.selection = "Second" })
Button("Press to goto 3rd view", action: { self.selection = "Third" })
Button("Press to goto 4th view", action: { self.selection = "Forth" })
Spacer()
}
}
.navigationBarTitle("Home")
}
}
它产生这个:
这是在 iPhone 11 模拟器上。
有人可以简单地解释一下为什么它们如此不同以及为什么我没有得到我想要的结果(这是一个带有标题、3 个按钮的主页 'screen',当在iPhone.
NavigationView
期望在 one 视图内(不是大量视图),因此以下是可能的解决方案(使用 Xcode 12 / [=19 进行测试=] 14):
struct ContentView: View
{
@State private var selection: String? = nil
var body: some View
{
NavigationView
{
VStack
{
Text("Content View")
Spacer()
Button("Press to goto 2nd view", action: { self.selection = "Second" })
Button("Press to goto 3rd view", action: { self.selection = "Third" })
Button("Press to goto 4th view", action: { self.selection = "Forth" })
Spacer()
}
.background(Group {
NavigationLink(destination: Text("Second View"), tag: "Second", selection: $selection) { EmptyView() }
NavigationLink(destination: Text("Third View"), tag: "Third", selection: $selection) { EmptyView() }
NavigationLink(destination: Text("Forth View"), tag: "Forth", selection: $selection) { EmptyView() }
})
.navigationBarTitle("Home")
}
}
}
我正在学习 SwiftUI 并遵循 Apple 的 'Landmark' 教程,然后学习示例并尝试使用这些知识制作自己的作品。
但是,我确实缺少一些理解,因为我不明白为什么我会出现 non-standard 行为。
苹果代码:
struct CategoryHome: View
{
var body: some View
{
NavigationView
{
Text("Landmarks Content")
.navigationBarTitle(Text("Featured"))
}
}
}
它产生这个:
我的代码:
struct ContentView: View
{
@State private var selection: String? = nil
var body: some View
{
NavigationView
{
NavigationLink(destination: Text("Second View"), tag: "Second", selection: $selection) { EmptyView() }
NavigationLink(destination: Text("Third View"), tag: "Third", selection: $selection) { EmptyView() }
NavigationLink(destination: Text("Forth View"), tag: "Forth", selection: $selection) { EmptyView() }
VStack
{
Text("Content View")
Spacer()
Button("Press to goto 2nd view", action: { self.selection = "Second" })
Button("Press to goto 3rd view", action: { self.selection = "Third" })
Button("Press to goto 4th view", action: { self.selection = "Forth" })
Spacer()
}
}
.navigationBarTitle("Home")
}
}
它产生这个:
这是在 iPhone 11 模拟器上。
有人可以简单地解释一下为什么它们如此不同以及为什么我没有得到我想要的结果(这是一个带有标题、3 个按钮的主页 'screen',当在iPhone.
NavigationView
期望在 one 视图内(不是大量视图),因此以下是可能的解决方案(使用 Xcode 12 / [=19 进行测试=] 14):
struct ContentView: View
{
@State private var selection: String? = nil
var body: some View
{
NavigationView
{
VStack
{
Text("Content View")
Spacer()
Button("Press to goto 2nd view", action: { self.selection = "Second" })
Button("Press to goto 3rd view", action: { self.selection = "Third" })
Button("Press to goto 4th view", action: { self.selection = "Forth" })
Spacer()
}
.background(Group {
NavigationLink(destination: Text("Second View"), tag: "Second", selection: $selection) { EmptyView() }
NavigationLink(destination: Text("Third View"), tag: "Third", selection: $selection) { EmptyView() }
NavigationLink(destination: Text("Forth View"), tag: "Forth", selection: $selection) { EmptyView() }
})
.navigationBarTitle("Home")
}
}
}