ios - 减少 SwiftUI 部分之间的表单间距

标签 ios swiftui swiftui-list swiftui-form

我正在尝试使用 SwiftUI 制作一个笔记应用程序,我想显示类似于 Apollo Reddit app 的笔记。做。

它显示帖子的方式并没有什么特别之处,它只是使用类似于带有 GroupedListStyle() 的列表的界面来显示帖子。 ,但部分之间的间距较小。

我尝试了很多技巧来减少这种间距,但似乎都没有奏效。

TL; 博士

我有这个 :enter image description here

我想要这个 :
enter image description here

任何帮助表示赞赏。提前致谢!

这是我的代码 :

import SwiftUI

struct NotesView: View {

    let array = [
        Note(title: "Mi pana miguel letra", content:
        """
        [Intro: Keyan JRN & Producer Tag]
        El pana Miguel, yah, ey
        El pana Miguel, yah, ey (Snorkatje)
        Mi pana, mi pana, yeah
        Mi pana, mi pana, yeah
        Mi pana, mi pana, yeah, eh-eh
        Uh-uh-uh-uh-uh-uh

        [Estribillo]
        Ha-ha-hace un rato conocí al pana Miguel
        No-no voy a mentir, se ve bastante fresco
        (Ey, tío, ¿conoces a IlloJuan?) ¿Quién?
        (IlloJuan) No, que quién te ha preguntado (No-oh)
        Ha-hace un rato conocí al pana Miguel (Pana Miguel)
        No voy a mentir, se ve bastante fresco (Bastante fresco)
        Y el desgraciado de Matías que se vaya ya (Uh-uh, uh, uh)
        Prefiero quedarme aquí con mi pana, sentado
        """
        ),
        Note(title: "Note 02", content: "This is a test note."),
        Note(title: "Note 03", content: "This is a test note that is supposed to be longer than just 3 lines to test the note preview. Since I cba to write...")
    ]

    @ObservedObject var searchBar: SearchBar = SearchBar()

    var body: some View {

        NavigationView {

            List {

                if array.count > 0 {
                    ForEach(
                        array.filter
                        {
                            searchBar.text.isEmpty ||
                                $0.id.localizedStandardContains(searchBar.text)
                        },
                        id: \.self
                    ) { eachNote in

                        Section {
                            NoteView(note: eachNote)
                        }.buttonStyle(PlainButtonStyle())

                    }
                } else {

                    NavigationLink(destination: NotesTextEditor()) {
                        Text("Create a new post")
                    }

                }


            }
            .listStyle(GroupedListStyle())

            .add(self.searchBar)

        }

    }

}

最佳答案

可能的解决方案是使用自定义 a-la 组分隔符,而不是标准。

在 Xcode 11.4/iOS 13.4 上对一些复制代码进行了测试。

demo

List {
    ForEach(array.indices, id: \.self) { i in
        VStack(spacing: 0) {
            Text(self.array[i].title)
                .padding(.horizontal)
            Text(self.array[i].content)
                .padding(.horizontal)

            if i != self.array.count - 1 { // don't show for last
                Rectangle().fill(Color(UIColor.systemGroupedBackground))
                   .frame(height: 16) // << fit as you need
            }
        }.listRowInsets(EdgeInsets()) // << avoid extra space
    }
}.listStyle(GroupedListStyle())

关于ios - 减少 SwiftUI 部分之间的表单间距,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62181622/

相关文章:

swiftui - 如何将图表中ChartDataEntry.init的字符串值设置为x?

ios - Swift UI 列表 : Open external URL when cell is tapped

ios - swift 3 : How to center horizontally the last row of cells in a UICollectionView with only one section?

ios - 如何调整 Maxpreps HTML 小部件的大小以适应 UIWebView?

android - Flex/Actionscript拖放/滑动效果(模拟android/ios界面)

swift - 是什么导致了 SwiftUI 和 NavigationView 的这个动画错误?

ios - 如何在 SwiftUI 中更改表单的背景颜色?

swiftui - 如何在 Swift UI 中不提供先前功能的情况下在自定义 View (ListRowView) 中添加按钮?

ios - 强制使用参数名称,但只是其中的一部分