swiftui - VStack 中的 NavigationLink 在 iPad 上已损坏
问题描述
考虑这个非常简单的视图:只有几个指向其他页面的链接。如果你在 iPhone 上运行它,一切都会按预期运行。但是,在 iPad 上横向运行它,您会注意到大多数时候链接根本不起作用。在sim和真机上都破了,这让人吃惊。
当您将 更改VStack
为 aList
时,一切都会突然正常。
struct ContentView: View {
var body: some View {
NavigationView {
VStack {
NavigationLink(destination: Text("Link 1")) {
Text("Link 1")
.padding(.vertical)
}
NavigationLink(destination: Text("Link 2")) {
Text("Link 2")
.padding(.vertical)
}
NavigationLink(destination: Text("Link 3")) {
Text("Link 3")
.padding(.vertical)
}
NavigationLink(destination: Text("Link 4")) {
Text("Link 4")
.padding(.vertical)
}
NavigationLink(destination: Text("Link 5")) {
Text("Link 5")
.padding(.vertical)
}
}
Text("Hello iPad")
}
}
}
有没有其他人遇到过这个问题?为什么会发生,解决方法是什么?除了明显的“使用列表”:p 我使用VStack
s 和HStack
s 的网格来显示多列中的链接,可根据设备大小进行配置。因此,如果不是 100% 必要,更改为 aList
将不是可取的。
解决方案
我建议您在尝试找到一些“解决方法”之前阅读文档
// Creates an instance that presents `destination` when `selection` is set
// to `tag`.
public init<V>(destination: Destination, tag: V, selection: Binding<V?>, @ViewBuilder label: () -> Label) where V : Hashable
示例用法
struct ContentView: View {
@State var selection: Int? = 0
var body: some View {
NavigationView {
VStack {
NavigationLink(destination: Text("Link 0"), tag: 0, selection: $selection) {
Text("link 0")
}
NavigationLink(destination: Text("Link 1"), tag: 1, selection: $selection) {
Text("link 1")
}
NavigationLink(destination: Text("Link 2"), tag: 2, selection: $selection) {
Text("link 2")
}
}
Text("Hello iPad user, swipe from left to continue ...")
}
}
}
推荐阅读
- java - 使用 java 8 流过滤和修改列表对象
- protractor - 如何从量角器中的外部函数返回整数作为承诺?
- c# - 在 C# 中使用线程异步执行语句
- javascript - 那么为什么不能在非异步函数中使用 await 呢?
- java - 为 ListView 索引设置存储的背景颜色时出现问题
- c++ - 智能指针、this 和构造函数
- java - Apache Lucene QueryParser.parse 未在 FuzzyQuery 上使用分析器
- python - 检查 pandas 数据框中的一系列单元格是否为 NaN 比使用 for 循环、math.isnan() 和 df.iloc() 更快的方法?
- android - 阴影映射在 Android 上的 OpenGL ES 3.0 中不起作用
- java - 无法在之间传递数据
testng 套件中的标签