【问题标题】:Spacing between sections in the same LazyVGrid?同一个 LazyVGrid 中的部分之间的间距?
【发布时间】:2021-07-19 09:35:24
【问题描述】:

可能是一个愚蠢的问题,我自己可能已经回答了这个问题。但是,我试图在我的网格部分之间放置一个空间。目前我正在使用 .padding 但这似乎并不理想。或者这真的是唯一的方法吗?

var body: some View {
  ScrollView(.vertical) {    
    LazyVGrid(
      columns: columns,
      alignment: .center,
      spacing: 3,
      pinnedViews: [.sectionHeaders, .sectionFooters]) {
        Section(header:Text("GRID 1").font(.title).bold().padding(.top, 10.0)) {
          ForEach(0...9, id: \.self) {
            index in Color(UIColor.random)
              .frame(minWidth: 0, maxWidth: .infinity, minHeight: 75, maxHeight: .infinity)
          }
        }

        Section(header:Text("GRID 2").font(.title).bold().padding(.top, 50.0)) {
          ForEach(10...20, id: \.self) {
            index in Color(UIColor.random)
              .frame(minWidth: 0, maxWidth: .infinity, minHeight: 75, maxHeight: .infinity)
          }
        }
      }
    }
}

【问题讨论】:

  • 这是否回答了您的问题stackoverflow.com/a/63027052/12299030
  • 感谢您的回复,不幸的是它没有。我在同一个 LazyVGrid 设置中有 2 个部分。基本上,我试图在“GRID 1”和“GRID 2”之间放置空格,我使用了填充并且效果很好,但它似乎不太理想,因为我真的不想要填充。我不确定 Spacer() 之类的东西是否会起作用。还没试过。

标签: swiftui


【解决方案1】:

如果我正确理解您的问题,在网格中的各个部分之间添加垂直空间的一种方法是在网格内添加具有明确长度的间隔(例如 Spacer(minLength: 45))。

struct ContentView: View {

  let gridItemLayout = [GridItem(.adaptive(minimum: 50, maximum: 50))]

  var body: some View {
    List {
      LazyVGrid(columns: gridItemLayout, spacing: 25) {
        SectionView()
        Spacer(minLength: 45)  <-------- VERTICAL SPACE BETWEEN SECTIONS
        SectionView()
      }
    }
  }
}

struct SectionView: View {

  var body: some View {
    Section() {
      Color.red.frame(height: 50)
      Color.black.frame(height: 50)
      Color.green.frame(height: 50)
      Color.blue.frame(height: 50)
      Color.orange.frame(height: 50)
      Color.pink.frame(height: 50)
      Color.gray.frame(height: 50)
    }
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-06-22
    • 2021-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-06
    • 2015-03-17
    相关资源
    最近更新 更多