的数据
预览屏幕中我的第三张卡顶部和第九张卡底部设置的背景颜色超过了卡的边框(我正在听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)
}
}
}
字符串
3条答案
按热度按时间tjvv9vkg1#
在
private func cardView
中为每个形状使用.clipped()
.字符串
xbp102n02#
在
.background(Color.green)
之后添加.clipped()
,例如:字符串
vsmadaxz3#
这是因为牌的顶部和底部与显示器顶部和底部的安全区域接触。当您使用圆括号应用背景色时,如下所示:
字符串
.然后它默认忽略安全区域-参见background(_:ignoresSafeAreaEdges:)。这就是颜色渗入安全区域的原因。
修复:
ignoresSafeAreaEdges
:型
ViewBuilder
作为参数,通常作为一个尾随闭包(带花括号)提供:型