下面你会看到我想要拉伸到红色边框的图像和黑色方块。
我已经尝试了以下
import SwiftUI
struct ContentView : View {
var body: some View {
HStack(spacing: 1) {
Rectangle().frame(width:20).foregroundColor(.red).frame(width:20)
ScrollView {
VStack {
ForEach(0..<5) { index in
Rectangle().frame(minWidth: 50, maxWidth: .infinity, minHeight: 50, maxHeight: 50)
}
}.relativeWidth(1)
}
Rectangle().foregroundColor(.red).frame(width:20)
}
}
}
#if DEBUG
struct ContentView_Previews : PreviewProvider {
static var previews: some View { ContentView() }
}
#endif
Run Code Online (Sandbox Code Playgroud)
但结果是这样的:
您可以使用GeometryReader并将您的 ScrollView 包装到其中并将内容宽度设置为几何的宽度大小。答GeometryReader:
将灵活的首选大小返回到其父布局。
因此,您的代码将如下所示:
HStack(spacing: 1) {
Rectangle().frame(width:20).foregroundColor(.red).frame(width:20)
GeometryReader { geometry in
ScrollView {
VStack {
ForEach(0..<5) { index in
Rectangle().frame(minWidth: 50, maxWidth: .infinity, minHeight: 50, maxHeight: 50)
}
}.relativeWidth(1)
.frame(width: geometry.size.width)
}
}
Rectangle().foregroundColor(.red).frame(width:20)
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2651 次 |
| 最近记录: |