我创建列表并在其中添加VStack,并在VStack中添加一些视图。当我运行项目时,我观察到List的滚动超出了安全区域。仅供参考,如果我删除了Frame属性,结果仍然相同。Simulator gif
struct ContentView : View {
var body: some View {
List(0..<5) { item in
HStack(alignment: VerticalAlignment.top, spacing: 5) {
Image(systemName: "photo")
VStack(alignment: HorizontalAlignment.leading, spacing: 10) {
Text("USA")
.font(.headline)
Text("This is an extremely long string that will never fit even the widest of Phones Excerpt From: Paul Hudson. “SwiftUI by Example”. Apple Books. ")
.lineLimit(nil)
.font(.subheadline)
}
}
}
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity)
.background(Color.red)
.onAppear() {
print("on Appear")
}.onDisappear() {
print("on Disappear")
}
}
}
答案 0 :(得分:1)
灵感来自谢克·谢赫(Shauket Sheikh)。您可以将.padding(.top)直接添加到列表中,然后完成。不需要VStack。
struct Pickup_EnRoute: Codable {
var name : String?
var sumValue: String?
var sumTotal: Double?
enum CodingKeys: String, CodingKey {
case name
case sumValue = "sumValue"
case sumTotal = "sumTotal"
}
}
答案 1 :(得分:0)
您还可以使用VStack
并设置其中的.padding()
。
代码:
struct ContentView : View {
var body: some View {
VStack {
List(0..<5) { item in
HStack(alignment: VerticalAlignment.top, spacing: 5) {
Image(systemName: "photo")
VStack(alignment: HorizontalAlignment.leading, spacing: 10) {
Text("USA")
.font(.headline)
Text("This is an extremely long string that will never fit even the widest of Phones Excerpt From: Paul Hudson. “SwiftUI by Example”. Apple Books. ")
.lineLimit(nil)
.font(.subheadline)
}
}
}
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity)
.background(Color.red)
.onAppear() {
print("on Appear")
}.onDisappear() {
print("on Disappear")
}
}.padding()
}
}