swiftui 如何在 2023 年删除导航链接列表上的 V 形?

问题描述 投票:0回答:1

是否有一个更干净的解决方案,通过这个答案这个答案来删除列表上的那些V形符号?也许今天苹果改进了它(甚至不使用navigationStack)这种问题。

我知道有同样的问题,但相对有点旧,我想知道今天是否有更好的解决方案,仍然不使用导航堆栈。第一个给出的解决方案似乎有点慢,尊重标准代码,而且有点太冗长。

我的起始代码

import SwiftUI

struct DevelopingView: View {
 


let test = ["first", "second"]

@State private var tappedItem: String?

var body: some View {
    
    NavigationView {
        List {
            //standard code with chevron
            Section {
                ForEach(test) { item in
                    NavigationLink(destination: Text("destination: \(item)") ) {
                        Text(item)
                    }
                }
            }
            //solution from apple site, but adding vstack elements are not "centered" vertically due to navigation link
            Section {
                ForEach(test) { item in
                    ZStack{
                        VStack(alignment: .leading) {
                            NavigationLink(destination: Text("destination: \(item)")) { EmptyView() }.opacity(0.0)
                            Text(item)
                        }
                    }
                }
            }
            
            //another solution i tested, but elements are "centered" and do not know how to adjust it
            Section {
                ForEach(test, id: \.self) { item in
                    ZStack {
                        NavigationLink(destination: Text("destination: \(item)")) {
                            EmptyView()
                        }
                        .opacity(0)
                        .buttonStyle(PlainButtonStyle())
                        
                        Text(item)
                    }
                }
            }
            
        }
    }
    //        solution found on stack
    NavigationView{
        List{
            ForEach(test) { item in
                Button(action: { tappedItem = item }) {   // << activate !!
                    Text("Message \(item)")
                        .padding(.vertical,3)
                }
            }
        }
        .background(
            NavigationLink(destination: Text("MessageDetailView \(tappedItem ?? "N/D")"),
                           isActive: Binding(
                            get: { tappedItem != nil },         // << handle !!
                            set: { _,_ in tappedItem = nil }
                           )){
                               EmptyView()
                           }
        )
    }
}
}


//gives Identifiable tho stringgs
extension String: Identifiable {
    public typealias ID = Int
    public var id: Int {
        return hash
    }
}
swiftui foreach swiftui-list swiftui-navigationlink swiftui-navigationview
1个回答
0
投票

在 iOS 17 中,您可以使用

navigationDestination(item:destination:)
将导航目的地绑定到变量。这意味着您可以在列表中包含
Button
,将变量设置为不同的值。

@State var item: String? = nil
List {
    Section {
        ForEach(test, id: \.self) { item in
            Button(item) {
                self.item = item
            }
            .tint(.primary) // only if you don't like the default tint of buttons
        }
    }
}
.navigationDestination(item: $item) { item in
    Text("destination: \(item)")
}

也就是说,当您有多种类型的数据(不仅仅是

String
)时,这会变得相当麻烦。您可能需要一个具有关联值的枚举,并在
navigationDestination
中打开它来计算它应该是哪个目的地。

旁注:您只需指定

ZStack
即可修复
alignment
解决方案中的对齐方式:

ZStack(alignment: .leading) {
    NavigationLink(destination: Text("destination: \(item)")) {
        EmptyView()
    }
    .opacity(0)
    Text(item)
}
© www.soinside.com 2019 - 2024. All rights reserved.