我不能在列表的同一行中使用多个 NavigationLink。
看起来导航堆栈完全搞砸了,因为你点击一次,它会转到多个视图,然后不规则地返回......
在 TestList 中,我尝试在 Sections 中添加单独的 NavigationLinks,并且尝试将 NavigationLinks 移动到视图层次结构中的两个不同位置...
我已经尝试为列表的每一行添加两个 NavigationViews,但是当我需要它时,navigationTitleBar 不会消失..
struct ContentView: View {
var body: some View {
NavigationView {
TestList()
}
}
}
struct TestList: View {
var body: some View {
List {
ListCellView()
}
}
}
struct ListCellView: View {
var body: some View {
VStack {
Spacer()
NavigationLink(destination: TestDestination1()) {
Text("Test Destination 1")
.frame(width: 140, height: 50)
.background(RoundedRectangle(cornerRadius: 7.0).strokeBorder(Color.green, lineWidth: 3.0))
}
Spacer()
NavigationLink(destination: TestDestination2()) {
Text("Test Destination 2")
.frame(width:140, height: 50)
.background(RoundedRectangle(cornerRadius: 7.0).strokeBorder(Color.purple, lineWidth: 3.0))
Spacer()
}
}
}
}
struct TestDestination1: View {
var body: some View {
Text("Test Destination 1")
}
}
struct TestDestination2: View {
var body: some View {
Text("Test Destination 2")
}
}
Run Code Online (Sandbox Code Playgroud)
我希望当您点击 NavigationLink 时,它会导航到目标视图。
发生的情况是,当两个 NavigationLinks 位于 List 的同一行中并且您点击它时,它将: 1. 转到其中一个视图 2. 点击“后退”后,它将带您返回该视图,然后执行你到另一个目的地视图。
正如其他人提到的,为什么在 1 个单元格中有 2 个 NavigationLinks。问题在于 Cell 的多个按钮和手势。我猜每个单元格预计最多 1 个 Button/NavigationLink。正如您所注意到的,在您的视频中,您点击了一个 NavigationLink,但您的整个 Cell 得到了手势(突出显示),这反过来会影响其他 Buttons/NavigationLinks。
无论如何,你可以让它在 1 个单元格中的 2 个 NavigationLinks 工作,并进行黑客攻击。下面我创建了 SGNavigationLink,我将其用于我自己的应用程序来解决您的问题。它只是替换了 NavigationLink 并基于 TapGesture,因此您将失去亮点。
注意:我稍微修改了您的 ListCellView,因为我的 SGNavigationLink 中的 Spacer 正在造成内部崩溃。
struct ListCellView: View {
var body: some View {
VStack {
HStack{
SGNavigationLink(destination: TestDestination1()) {
Text("Test Destination 1")
.frame(width: 140, height: 50)
.background(RoundedRectangle(cornerRadius: 7.0).strokeBorder(Color.green, lineWidth: 3.0))
}
Spacer()
}
HStack{
SGNavigationLink(destination: TestDestination2()) {
Text("Test Destination 2")
.frame(width:140, height: 50)
.background(RoundedRectangle(cornerRadius: 7.0).strokeBorder(Color.purple, lineWidth: 3.0))
}
Spacer()
}
}
}
}
struct SGNavigationLink<Content, Destination>: View where Destination: View, Content: View {
let destination:Destination?
let content: () -> Content
@State private var isLinkActive:Bool = false
init(destination: Destination, title: String = "", @ViewBuilder content: @escaping () -> Content) {
self.content = content
self.destination = destination
}
var body: some View {
return ZStack (alignment: .leading){
if self.isLinkActive{
NavigationLink(destination: destination, isActive: $isLinkActive){Color.clear}.frame(height:0)
}
content()
}
.onTapGesture {
self.pushHiddenNavLink()
}
}
func pushHiddenNavLink(){
self.isLinkActive = true
}
}
Run Code Online (Sandbox Code Playgroud)
我不确定为什么您需要多个导航链接(重复代码)。您可以使用一个数据源来保存列表所需的属性[标题、颜色、id 等],并根据 id 调用所需的视图。重复使用相同的代码。这是一个例子。
struct TestList: View {
var body: some View {
List { // <- Use Data source
ForEach(0..<2) { index in
ListCellView(index: index)
}
}
}
}
struct ListCellView: View {
var index: Int
var body: some View {
return NavigationLink(destination: ViewFactory.create(index)) {
Text("Test Destination 1")
.frame(width: 140, height: 50)
.background(RoundedRectangle(cornerRadius: 7.0).strokeBorder(Color.green, lineWidth: 3.0))
}
}
}
class ViewFactory {
static func create(_ index: Int) -> AnyView {
switch index {
case 0:
return AnyView(TestDestination1())
case 1:
return AnyView(TestDestination2())
default:
return AnyView(EmptyView())
}
}
}
struct TestDestination1: View {
var body: some View {
Text("Test Destination 1")
}
}
struct TestDestination2: View {
var body: some View {
Text("Test Destination 2")
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2543 次 |
最近记录: |