[英]How to create spacing between items in a SwiftUI List view?
我似乎找不到在List
项之间创建间距的方法。 也许我不应该一开始就列出清单?
你怎么看?
这是生成视图的代码:
struct ContentView: View {
@ObservedObject var ratesVM = RatesViewModel()
init() {
UINavigationBar.appearance().largeTitleTextAttributes = [.foregroundColor: UIColor.white]
UITableView.appearance().backgroundColor = UIColor.init(named: "midnight blue")
UITableViewCell.appearance().backgroundColor = .green
UITableView.appearance().separatorStyle = .none
}
var body: some View {
NavigationView {
List (ratesVM.ratesList, id: \.self) { rate in
Image(systemName: "plus")
Button(action: {print("pressed")}) {
Text(rate.hebrewCurrencyName)
}
}
.navigationBarTitle("המרות מטבע")
.navigationBarItems(leading:
Button(action: {
print("button pressed")
self.ratesVM.callService()
}) {
Image(systemName: "plus")
.foregroundColor(Color.orange)})
}.environment(\.layoutDirection, .rightToLeft)
}
}
您可以将最小列表行高定义为更大,这样行之间的间隔就会更大。
List (ratesVM.ratesList, id: \.self) { rate in
Image(systemName: "plus")
Button(action: {print("pressed")}) {
Text(rate.hebrewCurrencyName)
}
}.environment(\.defaultMinListRowHeight, 50) //minimum row height
或者,您可以将行构建为 HStack 并指定框架高度。
List (ratesVM.ratesList, id: \.self) { rate in
HStack {
Image(systemName: "plus")
Button(action: {print("pressed")}) {
Text(rate.hebrewCurrencyName)
}
}.frame(height: 50) //your row height
}.environment(\.defaultMinListRowHeight, 20)
或者作为 VStack 并使用 Spacers
List (ratesVM.ratesList, id: \.self) { rate in
VStack{
Spacer()
HStack {
Image(systemName: "plus")
Button(action: {print("pressed")}) {
Text(rate.hebrewCurrencyName)
}
}
Spacer()
}.frame(height: 50)
}.environment(\.defaultMinListRowHeight, 20)
SwiftUI 允许我们使用 padding() 修饰符在视图周围设置单独的填充。 如果您不带参数使用它,您将在所有方面获得系统默认填充,如下所示:
VStack {
Text("SwiftUI")
.padding()
Text("rocks")
}
但您也可以自定义要应用的填充量和位置。 因此,您可能只想将系统填充应用于一侧:
Text("SwiftUI")
.padding(.bottom)
或者您可能想要控制对所有边应用多少填充:
Text("SwiftUI")
.padding(100)
或者,您可以将两者结合起来,向视图的一侧添加特定数量的填充:
Text("SwiftUI")
.padding(.bottom, 100)
所以你可以做
Text(rate.hebrewCurrencyName)
.padding(50)
您可以通过删除列表行分隔符.listRowSeparator(.hidden)
并将列表行背景设置为定义顶部和底部 EdgeInsets 填充的 InsettableShape .listRowBackground()
来实现它。 顺便说一句,不要忘记将.listStyle(.plain)
设置为.plain
。
您可以在此开发帖子SwiftUI List Card View上找到更好的实现。
//
// ContentView.swift
// My App
//
// Created by Kolmar Kafran on 25/08/22.
// https://www.linkedin.com/in/kolmar-kafran/
//
import SwiftUI
struct ContentView: View {
@State private var someList = [0, 1, 2, 3, 4]
var body: some View {
List {
ForEach(someList, id: \.self) { n in
Text("\(n)")
.foregroundColor(.white)
.listRowBackground(
RoundedRectangle(cornerRadius: 5)
.background(.clear)
.foregroundColor(.blue)
.padding(
EdgeInsets(
top: 2,
leading: 10,
bottom: 2,
trailing: 10
)
)
)
.listRowSeparator(.hidden)
}
.onDelete { idx in
someList.remove(atOffsets: idx)
}
}
.listStyle(.plain)
}
}
填充不起作用,因为它只增加了项目/单元格的大小而不是它们之间的间距,但.environment(.defaultMinListRowHeight, 20)似乎有效
我还为按钮样式实现了一个自定义视图,以调整按钮相对于项目/单元格的框架和“可按下区域”。
struct MyButtonStyle: ButtonStyle {
func makeBody(configuration: Configuration) -> some View {
configuration.label
.foregroundColor(.black)
.opacity(configuration.isPressed ? 0.5 : 1.0)
.frame(width: 350, height: 50, alignment: Alignment.center)
.background(Color.orange)
}
}
struct ContentView: View {
@ObservedObject var ratesVM = RatesViewModel()
init() {
UINavigationBar.appearance().largeTitleTextAttributes = [.foregroundColor: UIColor.white]
UITableView.appearance().backgroundColor = UIColor.init(named: "midnight blue")
UITableViewCell.appearance().backgroundColor = .clear
UITableView.appearance().separatorStyle = .none
}
var body: some View {
NavigationView {
List (ratesVM.ratesList, id: \.self) { rate in
Button(action: {print("pressed")}) {
Text(rate.hebrewCurrencyName)
}.buttonStyle(MyButtonStyle())
}
.navigationBarTitle("המרות מטבע")
.navigationBarItems(leading:
Button(action: {
print("button pressed")
self.ratesVM.callService()
}) {
Image(systemName: "plus")
.foregroundColor(Color.orange)})
}.environment(\.layoutDirection, .rightToLeft)
.environment(\.defaultMinListRowHeight, 150)
}
}
这里的一个快速解决方案是将相应的listStyle
设置为SidebarListStyle
。 例如:
List {
Text("First row")
Text("Second row")
Text("Third row")
}
.listStyle(SidebarListStyle())
但请注意,在 macOS 和 iOS 上,侧边栏列表样式还在部分标题中显示披露指示符,允许用户折叠和展开部分。
另一种快速的替代方法是使用Divider()
辅助视图(即,可用于分隔其他内容的视觉元素):
List {
Text("First row")
Divider()
Text("Second row")
Divider()
Text("Third row")
}
这两种解决方案都不允许直接控制垂直间距,但至少提供了更宽敞的布局替代方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.