列表 SwiftUI 中的粘性页脚

Joh*_*110 9 swift swiftui

我正在尝试在 SwiftUI 的列表视图中实现粘性页脚

它的操作似乎与每个所说的标题不同。这是粘性标头实现的示例

     List {
      ForEach(0..<10) { index in
        Section(header: Text("Hello")) {
          ForEach(0..<2) { index2 in
            VStack {
              Rectangle().frame(height: 600).backgroundColor(Color.blue)
            }
          }
        }.listRowInsets(EdgeInsets())
      }
    }
Run Code Online (Sandbox Code Playgroud)

上面给出了粘性标头的情况。虽然,一旦我改变Section(header: ...它Section(footer:...,它似乎不再粘了,它只是放在行的末尾。

更明确的参考

 List {
  ForEach(0..<10) { index in
    Section(footer: Text("Hello")) {
      ForEach(0..<2) { index2 in
        VStack {
          Rectangle().frame(height: 600).backgroundColor(Color.blue)
        }
      }
    }.listRowInsets(EdgeInsets())
  }
}
Run Code Online (Sandbox Code Playgroud)

有人对此有任何解决方案吗?

Dou*_*ari 8

斯威夫特 5 / 2023 / iOS 16

List {
...
}
.toolbar {
  ToolbarItemGroup(placement: .bottomBar) {
     Text("App iOS Version 1.0.0 (xyz)")
         .font(.footnote)
         .foregroundColor(.secondary)
         .frame(maxWidth: .infinity)
         .textSelection(.enabled)
     }
}
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述


Joh*_*110 6

借助 SwiftUI (2) 的最新版本,我们现在可以访问更多 API

对于初学者,我们可以使用 aLazyVStack和 aScrollView来获得相当好的性能,然后我们可以使用pinnedViewsAPI 在数组中指定我们想要固定或粘性的补充视图。然后,我们可以使用包含内容的部分视图并指定页脚或页眉。

** 此代码从 Xcode beta 2 开始运行 **

至于在 a 中使用它,List我不太确定,看看Listvs 的性能会很有趣Lazy...

struct ContentView: View {
  var body: some View {
    ScrollView {
      LazyVStack(spacing: 10, pinnedViews: [.sectionFooters]) {
        ForEach(0..<20, id: \.self) { index in
          Section(footer: FooterView(index: index)) {
            ForEach(0..<6) { _ in
              Rectangle().fill(Color.red).frame(height: 100).id(UUID())
            }
          }
        }
      }
    }
  }
}

struct FooterView: View {
  
  let index: Int
  
  var body: some View {
    VStack {
      Text("Footer \(index)").padding(5)
    }.background(RoundedRectangle(cornerRadius: 4.0).foregroundColor(.green))
  }
}


struct ContentView_Previews: PreviewProvider {
  static var previews: some View {
    ContentView()
  }
}

Run Code Online (Sandbox Code Playgroud)