Ken*_*kin 6 listview ios swift swiftui
我似乎找不到在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)
}
}
Run Code Online (Sandbox Code Playgroud)
kaf*_*ran 11
您可以通过删除列表行分隔符.listRowSeparator(.hidden)并将列表行背景设置为.listRowBackground()定义顶部和底部 EdgeInsets 填充的 InsettableShape 来实现此目的。顺便说一句,不要忘记将.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)
}
}
Run Code Online (Sandbox Code Playgroud)
您可以将最小列表行高定义为更大,这样行之间的间隔就会更大。
List (ratesVM.ratesList, id: \.self) { rate in
Image(systemName: "plus")
Button(action: {print("pressed")}) {
Text(rate.hebrewCurrencyName)
}
}.environment(\.defaultMinListRowHeight, 50) //minimum row height
Run Code Online (Sandbox Code Playgroud)
或者,您可以将行构建为 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)
Run Code Online (Sandbox Code Playgroud)
或者作为 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)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9220 次 |
| 最近记录: |