首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >显示视图时,在NavigationLink上隐藏雪佛龙/箭头,SwiftUI

显示视图时,在NavigationLink上隐藏雪佛龙/箭头,SwiftUI
EN

Stack Overflow用户
提问于 2021-02-16 19:47:24
回答 3查看 4.2K关注 0票数 5

我正在尝试删除显示在屏幕右侧的雪佛龙,其中包含一个视图的navigationLink。以下是我的代码:

代码语言:javascript
运行
复制
        NavigationView {
        List {
             NavigationLink(destination: DynamicList()) {
                  ResultCard()
             }
      ...
      }

关于堆栈溢出的其他答案建议使用如下所示:

代码语言:javascript
运行
复制
NavigationLink(...)
   .opacity(0)

但是,这在我的情况下是行不通的,因为将不透明度降低到0也会删除我试图显示的视图。“隐藏”的情况也是如此。我到处搜索,我能找到的唯一可行的解决方案是改变填充,以“把”雪佛龙从侧面推下,但这是一个糟糕的解决方案,因为'ResultCard‘视图将出现摇摇欲坠/偏离中心在不同的显示器大小。

也许不可能删除雪佛龙--如果是这样的话,还有什么其他方法可以让用户点击“ResultCard”视图并被带到一个新页面,即不是通过导航链接?

我把头撞在墙上,所以任何想法都很感激。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2021-02-16 19:52:33

您可以在label视图上使用.overlay,并将NavigationLink设置为EmptyView()作为标签:

代码语言:javascript
运行
复制
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:另一种解决方案,它似乎可以处理除文本之外的其他类型的视图:

代码语言:javascript
运行
复制
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)
                    }
                })
            }
        }
    }
}
票数 8
EN

Stack Overflow用户

发布于 2021-12-18 18:11:33

下面是使用.background()的两个替代变体:

代码语言:javascript
运行
复制
// 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()
            }
        }
    }
}
代码语言:javascript
运行
复制
// 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()
        }
    }
}
代码语言:javascript
运行
复制
// 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
//    }
// }
票数 0
EN

Stack Overflow用户

发布于 2022-07-22 19:44:24

来自jnpdx的更新解决方案几乎适用于我,但它将动画混乱到下一个视图。下面是对我有用的内容(实际上比jnpdx的回答更简单):

代码语言:javascript
运行
复制
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)
                )
            }
        }
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66231181

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档