[英]How to move .listRowBackground style from the list to list element in SwiftUI?
This is my code:这是我的代码:
var body: some View {
List(months) { month in
NavigationLink {
MonthView(month: month)
} label: {
MonthElementView(month: month)
}
.listRowBackground(
Color(uiColor: mode.underlayBackgroundColor)
.clipped()
.cornerRadius(10)
)
}
.navigationTitle(months.first?.descriptiveYear ?? "")
}
This simply works as expected, but I need to use MonthElementView(month: month)
also here:这只是按预期工作,但我还需要在这里使用
MonthElementView(month: month)
:
var body: some View {
ScrollView {
VStack(alignment: .leading, spacing: 3, content: {
if let month = month {
MonthElementView(month: month)
} else {
Text("Nothing here yet")
}
})
}
}
and here it doesnt work.在这里它不起作用。 Is there a way to setup.listRowBackground style directly inside my
MonthElementView
?有没有办法直接在我的
MonthElementView
中设置 setup.listRowBackground 样式?
Here is the body for MonthElementView
:这是
MonthElementView
的主体:
var body: some View {
VStack(alignment: .center, spacing: 8, content: {
// some staff here
})
.frame(maxWidth: .infinity)
.padding(.top, 10)
.padding(.bottom, 10)
.padding(.leading, 2)
.padding(.trailing, 2)
}
background for list cell (as you have already):列表单元格的背景(正如您已经拥有的那样):
List(1..<13) { month in
MonthElementView(month: month)
// list background
.listRowBackground(
Color(.gray).opacity(0.5)
.clipped()
.cornerRadius(10)
)
}
background for Scollview element: Scollview 元素的背景:
ScrollView {
ForEach(1..<13) { month in
MonthElementView(month: month)
// scrollview background
.background(
Color(.orange).opacity(0.7)
.clipped()
.cornerRadius(10)
)
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.