问题描述
(MacOS 应用程序) 我正在尝试在图像旁边放置一个 lazyvgrid。使图像具有特定大小,并让 LazyVGrid 占据其余的宽度。我设置了图像的框架,但网格没有占用额外的空间,图像和网格的左侧和右侧有额外的空白空间。
import SwiftUI
struct ContentView: View {
var body: some View {
HStack{
LazyVGrid(columns: [GridItem(
.adaptive(minimum: 40)
)]){
Text("Placeholder")
Text("Placeholder")
}
.padding()
.background(Color.blue)
Image("bla")
.resizable()
//.scaledToFit()
.frame(width: 100,height: 100)
.background(Color.pink)
}
}
}
解决方法
尝试这样的事情:
struct ContentView: View {
var body: some View {
HStack {
LazyVGrid(columns: [GridItem(.adaptive(minimum: 40))]){
Text("Placeholder")
Text("Placeholder")
}
.padding()
// put this here if you want the width and the height to expand
// .frame(maxWidth: .infinity,maxHeight: .infinity,alignment: .leading)
.background(Color.blue)
// put this here if you want just the width to expand
.frame(maxWidth: .infinity,alignment: .leading)
Image(systemName: "globe")
.resizable()
//.scaledToFit()
.frame(width: 100,height: 100)
.background(Color.pink)
}.frame(maxWidth: .infinity,alignment: .leading)
}
}