如何在 SwiftUI 中创建彼此分开的列表行
How to create List row seperate one another in SwiftUI
我是 SwiftUI 的新手
我正在尝试创建这样的部分,其中每一行都与另一行分开。有点像背景没有相互连接的部分。看图:(来自dribbble的图片)
但我的结局是这样的:
(我创建了蓝色背景,以便大家可以清楚地看到行)
这是我的代码:
import SwiftUI
struct ProductPageView: View {
init() {
UITableView.appearance().backgroundColor = .clear // Uses UIColor
}
var body: some View {
NavigationView {
ZStack {
Color.blue.edgesIgnoringSafeArea(.all)
VStack {
List {
ForEach(arrayProduct, id: \.name) { dataProduct in
ProductListCell(item: dataProduct)
}
.listRowInsets(EdgeInsets(top: 10, leading: 10, bottom: 10, trailing: 10))
}
.listStyle(InsetGroupedListStyle())
}
}
.navigationTitle("Produk")
}
}
}
我用过 listRowInsets
但它只是拉伸而已。
如何在行之间创建分隔?
任何帮助将不胜感激。
谢谢
我没有尝试像素完美复制,因为我没有资产,但下面概述了一种可以实现此目的的方法。
注释在代码中解释了一些关键部分:
import SwiftUI
struct ContentView: View {
var body: some View {
NavigationView {
ZStack {
Color.blue.edgesIgnoringSafeArea(.all)
VStack {
// This handles the display of the
// section header
HStack {
// The text & spacer are grouped
// so that you can pad accordingly
Text("Stuff")
Spacer()
}
.padding(.bottom, 2)
.padding(.leading, 10)
// A VStack contains your list of items
VStack {
ForEach(0...3, id: \.self) { element in
// Each grouping (a product for you)
// will exist within this top level
// HStack
HStack {
// A new HStack (or another view of
// your choice) will contain the views
// that compose your product entry
HStack {
Text("\(element)")
Spacer()
}
.padding() // Provides some buffer around the product
.background(Color.white)
.contentShape(Rectangle()) // For tapping
.cornerRadius(5.0)// Rounding!
}
// Custom padding can tailor your spacing needs
.padding([.top, .bottom], 2)
.padding([.trailing, .leading], 10)
}
}
Spacer()
}
}
.navigationTitle("Produk")
}
}
}
我是 SwiftUI 的新手
我正在尝试创建这样的部分,其中每一行都与另一行分开。有点像背景没有相互连接的部分。看图:(来自dribbble的图片)
但我的结局是这样的: (我创建了蓝色背景,以便大家可以清楚地看到行)
这是我的代码:
import SwiftUI
struct ProductPageView: View {
init() {
UITableView.appearance().backgroundColor = .clear // Uses UIColor
}
var body: some View {
NavigationView {
ZStack {
Color.blue.edgesIgnoringSafeArea(.all)
VStack {
List {
ForEach(arrayProduct, id: \.name) { dataProduct in
ProductListCell(item: dataProduct)
}
.listRowInsets(EdgeInsets(top: 10, leading: 10, bottom: 10, trailing: 10))
}
.listStyle(InsetGroupedListStyle())
}
}
.navigationTitle("Produk")
}
}
}
我用过 listRowInsets
但它只是拉伸而已。
如何在行之间创建分隔?
任何帮助将不胜感激。
谢谢
我没有尝试像素完美复制,因为我没有资产,但下面概述了一种可以实现此目的的方法。
注释在代码中解释了一些关键部分:
import SwiftUI
struct ContentView: View {
var body: some View {
NavigationView {
ZStack {
Color.blue.edgesIgnoringSafeArea(.all)
VStack {
// This handles the display of the
// section header
HStack {
// The text & spacer are grouped
// so that you can pad accordingly
Text("Stuff")
Spacer()
}
.padding(.bottom, 2)
.padding(.leading, 10)
// A VStack contains your list of items
VStack {
ForEach(0...3, id: \.self) { element in
// Each grouping (a product for you)
// will exist within this top level
// HStack
HStack {
// A new HStack (or another view of
// your choice) will contain the views
// that compose your product entry
HStack {
Text("\(element)")
Spacer()
}
.padding() // Provides some buffer around the product
.background(Color.white)
.contentShape(Rectangle()) // For tapping
.cornerRadius(5.0)// Rounding!
}
// Custom padding can tailor your spacing needs
.padding([.top, .bottom], 2)
.padding([.trailing, .leading], 10)
}
}
Spacer()
}
}
.navigationTitle("Produk")
}
}
}