由于不同大小的设备可能对其 Widget 使用不同的半径,而子视图往往也需要同步修改,这无疑是比较麻烦的。Appe新增的 ContainerRelativeShape 是一种新的形状类型,它将采用最接近父视图容器形状的路径,并根据形状的位置使用适当的角半径。
自动使拐角半径与其父形状一致,而无需指定cornerRadius
效果:

struct ContentView: View {
var body: some View {
VStack {
Circle().fill(Color.red).frame(width: 50, height: 50, alignment: .center)
Circle().fill(Color.yellow).frame(width: 100, height: 100, alignment: .center)
}.background(ContainerRelativeShape()
.inset(by: 0)//边距,相当于设置padding
.fill(Color.blue))
}
}
网友评论