我有一個導航視圖串列,其中串列中的每個專案都有一個標題、2 行子文本和一個部分填充的矩形。行看起來像我想要的那樣,但是串列項不夠大以適合整個視圖,因此它們向外流動并相互重疊。這是它的外觀示例:

那么我怎樣才能讓串列項擴展到他們孩子的大小呢?
這是我的代碼:
struct ItemDetials {
let title: String
let rowOne: String
let rowTwo: String
let stat: Double
let maxOfStat: Double
let color: UIColor
}
struct ItemDetailsRowView: View {
var itemDetails: ItemDetials
var body: some View {
GeometryReader { geometry in
ZStack(alignment: .leading) {
Rectangle().frame(width: geometry.size.width * itemDetails.stat/itemDetails.maxOfStat,
height: geometry.size.height)
.opacity(0.3)
.foregroundColor(Color(itemDetails.color))
HStack {
VStack(alignment: .leading) {
Text(itemDetails.title)
.font(.system(size: 20))
Text(itemDetails.rowOne)
.font(.system(size: 12))
.foregroundColor(.gray)
Text(itemDetails.rowTwo)
.font(.system(size: 12))
.foregroundColor(.gray)
}
}
}
}
}
}
struct ItemDetailsRowView_Previews: PreviewProvider {
static var previews: some View {
let details: [ItemDetials] = [
ItemDetials(
title: "Test Title",
rowOne: "Some info about row",
rowTwo: "Some more info",
stat: 0.6,
maxOfStat: 1.0,
color: .red
),
ItemDetials(
title: "Test Title 2",
rowOne: "Some info about row 2",
rowTwo: "Some more info 2",
stat: 0.2,
maxOfStat: 1.0,
color: .green
),
ItemDetials(
title: "Test Title 3",
rowOne: "Some info about row 3",
rowTwo: "Some more info 3",
stat: 0.8,
maxOfStat: 1.0,
color: .blue
)
]
VStack {
let withIndex = details.enumerated().map({ $0 })
List {
ForEach(withIndex, id: \.element.title) { index, _ in
NavigationLink(destination: Text("Sup"), label: {
ItemDetailsRowView(itemDetails: details[index])
})
}
}
}
}
}
uj5u.com熱心網友回復:
相反包裹的整個 ZStack用GeometryReader,只有換Rectangle地方你需要的大小:
struct ItemDetailsRowView: View {
var itemDetails: ItemDetials
var body: some View {
ZStack(alignment: .leading) {
GeometryReader { geometry in
Rectangle().frame(width: geometry.size.width * itemDetails.stat/itemDetails.maxOfStat,
height: geometry.size.height)
.opacity(0.3)
.foregroundColor(Color(itemDetails.color))
}
HStack {
VStack(alignment: .leading) {
Text(itemDetails.title)
.font(.system(size: 20))
Text(itemDetails.rowOne)
.font(.system(size: 12))
.foregroundColor(.gray)
Text(itemDetails.rowTwo)
.font(.system(size: 12))
.foregroundColor(.gray)
}
}
}
}
}

轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/344342.html
上一篇:快速劃分textField
