我正在尝试删除显示在屏幕右侧的雪佛龙,其中包含一个视图的navigationLink。以下是我的代码:
NavigationView {
List {
NavigationLink(destination: DynamicList()) {
ResultCard()
}
...
}
关于堆栈溢出的其他答案建议使用如下所示:
NavigationLink(...)
.opacity(0)
但是,这在我的情况下是行不通的,因为将不透明度降低到0也会删除我试图显示的视图。“隐藏”的情况也是如此。我到处搜索,我能找到的唯一可行的解决方案是改变填充,以“把”雪佛龙从侧面推下,但这是一个糟糕的解决方案,因为'ResultCard‘视图将出现摇摇欲坠/偏离中心在不同的显示器大小。
也许不可能删除雪佛龙--如果是这样的话,还有什么其他方法可以让用户点击“ResultCard”视图并被带到一个新页面,即不是通过导航链接?
我把头撞在墙上,所以任何想法都很感激。
发布于 2021-02-16 19:52:33
您可以在label视图上使用.overlay
,并将NavigationLink
设置为EmptyView()
作为标签:
struct ContentView : View {
var body: some View {
NavigationView {
List {
NavigationLink("Link 1", destination: Text("Hi"))
Text("Test")
.overlay(NavigationLink(destination: Text("Test"), label: {
EmptyView()
}))
}
}
}
}
Update:另一种解决方案,它似乎可以处理除文本之外的其他类型的视图:
struct ContentView : View {
@State private var linkActive = false
var body: some View {
NavigationView {
List {
NavigationLink("Link 1", destination: Text("Hi"))
Button(action: { linkActive = true }) {
Image(systemName: "pencil")
}.overlay(VStack {
if linkActive {
NavigationLink(destination: Text("Test"), isActive: $linkActive) {
EmptyView()
}.opacity(0)
}
})
}
}
}
}
发布于 2021-12-18 18:11:33
下面是使用.background()的两个替代变体:
// Replicate the iPhone Favorites tab with the info button
// - Compose a button to link from a NavigationView to a next view
// - Use this when you want to hide the navigation chevron decoration
// - and/or to have a button trigger the link
struct NavigationLinkButton<Destination: View, Label: View>: View {
@Binding var selectedID: String?
@State var rowID: String
@ViewBuilder var destination: () -> Destination
@ViewBuilder var label: () -> Label
var body: some View {
HStack {
Spacer()
Button {
selectedID = rowID
} label: {
label()
}
.buttonStyle(.plain) // prevent List from processing *all* buttons in the row
.background {
NavigationLink("", tag: rowID, selection: $selectedID) {
destination()
}
.hidden()
}
}
}
}
// Replicate the iOS Spotlight search for contacts with action buttons
// - Compose a list row to link from a NavigationView to a next view
// - Use this when you want to hide the navigation chevron decoration
// - and add action buttons
struct NavigationLinkRowHidingChevron<Destination: View, Label: View>: View {
@Binding var selectedID: String?
@State var rowID: String
@ViewBuilder var destination: () -> Destination
@ViewBuilder var label: () -> Label
var body: some View {
ZStack {
// Transparent button to capture taps across the entire row
Button("") {
selectedID = rowID
}
label()
}
.background {
NavigationLink("", tag: rowID, selection: $selectedID) {
destination()
}
.hidden()
}
}
}
// Example Usages
//
// @State private var selectedID: String?
// @State private var editMode: EditMode = .inactive
//
// ... and then in the body:
// List {
// ForEach(items) { item in
// row(for: item)
// .swipeActions(edge: .leading, allowsFullSwipe: true) {
// ...
// }
// .contextMenu {
// ...
// }
// .onDrag({
// ...
// })
// // Overlay so that a tap on the entire row will work except for these buttons
// .overlay {
// // Hide info button just as with Phone Favorites edit button
// if editMode == .inactive {
// NavigationLinkHidingChevron(selectedID: $selectedID, rowID: item.id) {
// // Destination view such as Detail(for: item)
// } label: {
// // Button to activate nav link such as an button
// }
// }
// }
// }
// .onDelete(perform: deleteItems)
// .onMove(perform: moveItems)
// }
//
// ... or this in the body:
// NavigationLinkHidingChevron(selectedID: $selectedID, rowID: contact.id) {
// // Destination view such as Detail(for: item)
// } label: {
// // Content for the list row
// }
// .contextMenu {
// ...
// }
// .overlay {
// HStack(spacing: 15) {
// // Right-justify the buttons
// Spacer()
// // Buttons that replace the chevron and take precedence over the row link
// }
// }
发布于 2022-07-22 19:44:24
来自jnpdx的更新解决方案几乎适用于我,但它将动画混乱到下一个视图。下面是对我有用的内容(实际上比jnpdx的回答更简单):
struct ContentView : View {
@State private var linkActive = false
var body: some View {
NavigationView {
List {
Button(action: { linkActive = true }) {
Image(systemName: "pencil")
}.overlay(
NavigationLink(
isActive: $linkActive,
destination: { Text("Test") },
label: { EmptyView() }
).opacity(0)
)
}
}
}
}
https://stackoverflow.com/questions/66231181
复制相似问题