SwiftUI父级高度



在SwiftUI中,如何给父视图高度0并隐藏所有子视图(因为父视图高度为0(。此代码仍然显示ZStack。它比孩子们高。

ZStack {
RoundedRectangle(cornerRadius: 10.0, style: /*@START_MENU_TOKEN@*/.continuous/*@END_MENU_TOKEN@*/)
.fill(Color.red)
Image(systemName: "checkmark.circle.fill")
.foregroundColor(.white)
}
.frame(maxHeight: 0)
.animation(.spring())

您只需要在设置帧后进行剪辑,因为任何堆栈都只是透明容器,应用的帧实际上是0高度,但内容可以通过它看到,所以解决方案是

ZStack {
RoundedRectangle(cornerRadius: 10.0, style: /*@START_MENU_TOKEN@*/.continuous/*@END_MENU_TOKEN@*/)
.fill(Color.red)

Image(systemName: "checkmark.circle.fill")
.foregroundColor(.white)
}
.frame(maxHeight: 0)
.clipped()                   // << here !!
.animation(.spring())

最新更新