我有一个macOS应用程序,有三列视图。在第一列中,有一个
List
有很多很长的东西,可能有几百件。
如果我的
NavigationLink
每个项目包含一个
isActive
参数,当单击NavigationLinks时,列表上会出现不可预测/不需要的滚动行为。
例如,如果我向下滚动到项目100并单击它,则
列表
也许
决定向上滚动至第35项左右(其中
导航链接
不在画面中)。这种行为似乎有点不确定——它并不总是滚动到同一个位置。看来
不太可能
如果我在列表中滚动到所需的项目,然后等待系统滚动条消失,然后单击
导航链接
,但这并不能使问题完全消失。
如果我移除
活跃的
参数时,当单击
导航链接
s
struct ContentView : View {
var body: some View {
NavigationView {
SidebarList()
Text("No selection")
Text("No selection")
.frame(minWidth: 300)
}
}
}
struct Item : Identifiable, Hashable {
let id = UUID()
var name : String
}
struct SidebarList : View {
@State private var items = Array(0...300).map { Item(name: "Item \($0)") }
@State private var activeItem : Item?
func navigationBindingForItem(item: Item) -> Binding<Bool> {
.init {
activeItem == item
} set: { newValue in
if newValue { activeItem = item }
}
}
var body: some View {
List(items) { item in
NavigationLink(destination: InnerSidebar(),
isActive: navigationBindingForItem(item: item)
) {
Text(item.name)
}
}.listStyle(SidebarListStyle())
}
}
struct InnerSidebar : View {
@State private var items = Array(0...100).map { Item(name: "Inner item \($0)") }
var body: some View {
List(items) { item in
NavigationLink(destination: Text("Detail")) {
Text(item.name)
}
}
}
}
我想继续
活跃的
,因为我有一些程序导航,我想能够做到这取决于它。例如:
Button("Go to item 10") {
activeItem = items[10]
}
有没有办法使用
活跃的
上
导航链接
避免不可预知的滚动行为?
(使用macOS 11.3和Xcode 13.0构建和测试)