我有一堆
Buttons
在里面
LazyVGrid
如下图所示。我想在按钮的左上角附近显示一个数字。对于特定的iOS设备型号,我只能偏移一个数字的位置。但如果我有一台iPad,这种方法可能会大错特错。有没有办法得到的大小
GridItem
View
? 如果没有,有没有一种有效的方法可以在左上角附近显示数字,而不管设备型号如何?谢谢
import SwiftUI
struct ContentView: View {
private let cols = [
GridItem(.flexible()),
GridItem(.flexible()),
GridItem(.flexible()),
GridItem(.flexible()),
GridItem(.flexible()),
GridItem(.flexible()),
GridItem(.flexible())
]
var body: some View {
LazyVGrid(columns: cols) {
ForEach(0...30, id: \.self) { item in
Button {
} label: {
Image(systemName: "\(item).circle.fill")
.resizable()
.aspectRatio(contentMode: .fit)
.foregroundColor(.orange)
}
.overlay {
Text("0")
.font(.caption)
.foregroundStyle(.blue)
.offset(x: -20, y: -20)
}
}
}
}
}