我有一个带有 NavigationLinks 的列表。
List {
ForEach(items, id: \.id) { item in
NavigationLink(destination: ItemView(), tag: item.id, selection: self.$viewModel.selectedItemId) {
Text("Some text")
}
}
.onDelete(perform: delete)
}
.id(UUID())
还有一个相应的 ViewModel,它存储了所选项目的 ID。
class ViewModel: ObservableObject {
@Published var selectedItemId: String? {
didSet {
if let itemId = selectedItemId {
...
}
}
}
...
}
问题是,当我使用 NavigationLink(destination:tag:selection:)
时,过渡动画消失了—— subview 立即弹出。当我使用 NavigationLink(destination:)
时,它可以正常工作,但我无法使用它,因为我需要在选择 NavigationLink 时执行一些操作。
为什么过渡动画没有了?这是 NavigationLink(destination:tag:selection:)
的问题吗?
最佳答案
您可以将您的操作放在 NavigationLink 中。这应该可以解决您的动画问题:
List {
ForEach(items, id: \.id) { item in
NavigationLink(destination: ItemView(), isActive: $isActive, tag: item.id, selection: self.$viewModel.selectedItemId) {
EmptyView()
}
Button(action: {
// The action you wand to have done, when the View pops.
print("Test")
self.isActive = true
}, label: {
Text("Some text")
})
}
.onDelete(perform: delete)
}
.id(UUID())
关于ios - 在 SwiftUI 中呈现 NavigationLink 时过渡动画消失了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61988298/