预览画面中我的第三张卡的顶部和第九张卡的底部设置的背景颜色超出了卡的边框(我正在听CS193P)
这是我的代码
import SwiftUI
struct CardView: View {
let cardShape: CardViewShape
let color: Color
let elementsNum: Int
let fillingMethod: FillingMethod
var body: some View {
GeometryReader { geometry in
VStack(alignment: .center, spacing: 5) {
Spacer()
ForEach(1..<elementsNum+1, id:\.self) {index in
cardView(for: cardShape, in: geometry)
}
Spacer()
}
.frame(width: geometry.size.width)
.border(Color.orange, width: 5)
.background(Color.green)
}
.aspectRatio(2/3, contentMode: .fit)
}
@ViewBuilder
private func cardView(for cardShape: CardViewShape, in geometry: GeometryProxy) -> some View {
switch (cardShape) {
case .circle:
let width = min(geometry.size.width, geometry.size.height/CGFloat(elementsNum)) - 10
Circle()
.fill(mode: fillingMethod, color: color)
.frame(maxWidth: width)
case .rectangle:
let width = geometry.size.width * 0.65
let height = geometry.size.height / 5
Rectangle()
.fill(mode: fillingMethod, color: color)
.frame(width: width, height: height)
case .diamond:
Diamond()
.fill(mode: fillingMethod, color: color)
}
}
}
尝试在
.clipped()
之后添加 .background(Color.green)
。