【问题标题】:SwiftUI NavigationView caret positionSwiftUI NavigationView 插入符号位置
【发布时间】:2021-03-29 23:43:17
【问题描述】:

希望找到一种方法让 NavigationView 插入符号与行的右上角与中心对齐。希望找到一种方法使插入符号与我的文章标题保持一致。

List {
    ForEach(searching ? faqTableSearch : faqTable) { section in
        Section(header: Text(section.section)) {
            ForEach(section.faq) { row in
                NavigationLink(destination: FaqView(title: row.title, summary: row.summary, content: row.body)) {
                    VStack {
                        Text(row.title)
                            .font(.custom("Helvetica", size: 16))
                            .foregroundColor(Color(UIColor.systemGray))
                            .offset(x: -16)
                        Spacer()
                        Text(row.summary)
                            .font(.custom("Helvetica", size: 12))
                            .foregroundColor(Color(UIColor.systemGray))
                            .offset(x: 16)
                    }
                } // end nav
            } // end item
        } // end section
        .font(.custom("Helvetica", size: 17))
    } // end loop
}

【问题讨论】:

    标签: swiftui swiftui-list


    【解决方案1】:

    我认为您不能将NavigationLink 创建的标准人字形移动到NavigationView 中,但您可以隐藏默认NavigationLink 并使用(略有不同的SF 符号chevron.rightchevron.forward):

    struct ContentView: View {
        var items = ["Item 1", "Item 2", "Item 3"]
        
        var body: some View {
            NavigationView {
                List {
                    ForEach(items, id: \.self) { item in
                        ZStack {
                            NavigationLink(destination: Text("Detail")) { EmptyView() }.hidden()
                            VStack(alignment: .leading) {
                                HStack {
                                    Text(item)
                                        .font(.custom("Helvetica", size: 16))
                                        .offset(x: -16)
                                    Spacer()
                                    Image(systemName: "chevron.right")
                                }.foregroundColor(Color(UIColor.systemGray))
                                
                                Spacer()
                                Text(item)
                                    .font(.custom("Helvetica", size: 12))
                                    .foregroundColor(Color(UIColor.systemGray))
                                    .offset(x: 16)
                            }
                        }
                    }
                }
            }
        }
    }
    

    【讨论】:

    • 这让我走了 99% 的路,但它仍然同时显示旧的导航插入符号,所以添加 .frame 和 .hidden 就可以了: NavigationLink(destination: FaqView(title: row.title,摘要:row.summary,内容:row.body)) { EmptyView() } .frame(width: 0, height: 0) .hidden()
    【解决方案2】:

    Image(systemName: "chevron.right").font(.system(size: 14)).opacity(0.3)

    enter image description here

    var body: some View {
        NavigationView {
            List {
                Section(header: GroupHeader(title: "Example")) {
                   NavigationLink(
                        destination: UnderlineTextField_Example(),
                        label: {
                            Text("TextField")
                        }
                    )
                    
                    HStack {
                        Button("Navigation") {
                            isPresented.toggle()
                        }
                        .fullScreenCover(isPresented: $isPresented) {
                            TabNavigation()
                        }
                        
                        Spacer()
                        
                        Image(systemName: "chevron.right")
                            .font(.system(size: 14))
                            .opacity(0.3)
                    }
            }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-10-06
      • 1970-01-01
      • 2022-08-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-20
      • 2013-07-18
      相关资源
      最近更新 更多