【问题标题】:How to put LazyVGrid next to Image swiftUI on MacOS (11.4)如何在 MacOS (11.4) 上将 LazyVGrid 放在 Image swiftUI 旁边
【发布时间】:2021-06-28 03:25:33
【问题描述】:

(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)
        }
    }
}

【问题讨论】:

  • HStack 间距仅考虑它们之间的小空间,而不是左右空间。
  • 尝试将.frame(maxWidth: .infinity) 添加到HStack
  • 将其添加到网格中使其工作

标签: swift swiftui lazyvgrid


【解决方案1】:

试试这样的:

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, maxHeight: .infinity, alignment: .leading)

            Image(systemName: "globe")
                .resizable()
                //.scaledToFit()
                .frame(width: 100, height: 100)
                .background(Color.pink)

        }.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .leading)
    }
}

【讨论】:

    猜你喜欢
    • 2020-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-03
    • 2020-02-22
    • 1970-01-01
    • 2023-02-15
    • 2013-05-15
    相关资源
    最近更新 更多