swiftui - 如何在视图中隐藏超大的背景视图?
问题描述
我有一个简单的矩形,我在它的背景中放了一个数字的 VStack,你可以在代码和图片中看到它!问题是 VStack 比 Rectangle 高,因为它超出了 Rectangle,我希望隐藏额外的部分,我该如何解决?
struct ContentView: View {
let arrayOfHours: [Int] = Array(0...23)
var body: some View {
Rectangle()
.fill(Color.black.opacity(0.25))
.frame(height: 200, alignment: .center)
.padding()
.background(
VStack(alignment: .center, spacing: 4) {
ForEach (arrayOfHours.indices, id: \.self) { index in
Text(arrayOfHours[index].description)
.font(Font.body.bold())
}
}
.background(Color.yellow)
)
}
}
解决方案
如果我正确理解了你的描述,你需要clipped
,比如
struct ContentView: View {
let arrayOfHours: [Int] = Array(0...23)
var body: some View {
Rectangle()
.fill(Color.black.opacity(0.25))
.frame(height: 200, alignment: .center)
.background(
VStack(alignment: .center, spacing: 4) {
ForEach (arrayOfHours.indices, id: \.self) { index in
Text(arrayOfHours[index].description)
.font(Font.body.bold())
}
}
.background(Color.yellow)
)
.clipped() // << here !!
.padding() // should be moved here !!
}
}
推荐阅读
- android - Android 9.0 上的 Flutter FCM 无声音
- python - 我想请求一个链接,但是有“Traceback(最近一次通话最后一次)”
- android - 使用 Picaso 加载并链接到布局文件后,ImageView 图像未显示
- python - 我该如何解决以下这些错误?
- r - 从 SparkR 读取 Kudu
- java - 如何转换列表
> 进入列表 - excel - 在 VBA 中的子类实例中公开父类的方法
- reactjs - 反应:打字稿 - 忽略分配接口
- makefile - 使用 esp-iot-solution 存储库时,esp-idf 路径不会覆盖
- javascript - 如何使用 Chrome 76 调试等待功能?