【问题标题】:SwiftUI re-orderable non full screen listSwiftUI 可重新排序的非全屏列表
【发布时间】:2020-12-23 12:36:16
【问题描述】:

预期目标是扩展高度以显示其所有元素(无滚动)的视图,可以允许拖放其元素以重新排序它们。我相信 SwiftUI 列表将支持这种拖放行为,但是当与其他元素一起放置在 VStack 中时,它会将其高度限制为一个元素的大小。我认为这些列表视图不打算在另一个视图中使用?

List {
   ForEach(viewModel.sections, id: \.self) { section in
      HStack {
         Text(section.localisedString)
         Spacer()
         Image(systemName: "line.horizontal.3")
            .foregroundColor(ColourPalette.bodyText)
            .smallIcon()
      }
      .background(ColourPalette.background)
      .listRowInsets(EdgeInsets(top: 0, leading: 0, bottom: 0, trailing: 0))
   }
   .background(ColourPalette.background)
}
.listStyle(PlainListStyle())
.background(ColourPalette.background)

我也想隐藏分隔符,但这似乎是 SwiftUI 的持续挣扎,但即使是 SideBarListStyle() 也不适合我。

使用VStack() 是否更有意义?如果是这样,实现拖放和经典列表侧栏项目的重新排序有多容易?

【问题讨论】:

  • 也许在List 中使用Sectionheaderfooter 可以解决您的问题

标签: swiftui swiftui-list


【解决方案1】:

你有两个选择:

1) 使用List

要将VStack 与您的List 一起放置,请将Sectionheader / footer 一起使用,这样它将随着列表滚动

List {
    Section(header: Text("Your header here")) {
        ForEach(viewModel.sections, id: \.self) { section in
            HStack {
                Text(section.localisedString)
                Spacer()
                Image(systemName: "line.horizontal.3")
                    .foregroundColor(ColourPalette.bodyText)
                    .smallIcon()
            }
            .background(ColourPalette.background)
            .listRowInsets(EdgeInsets(top: 0, leading: 0, bottom: 0, trailing: 0))
        }
        .background(ColourPalette.background)
    }
}

对于删除分隔符(IOS 13 支持):

List 出现之前在代码中的任何位置添加UITableView.appearance().separatorColor = .clear 应该可以工作。虽然此解决方案删除了​​分隔符,但请注意,所有 List 实例都将绑定到此样式,因为目前没有官方方法可以仅删除特定实例的分隔符。您可以在onAppear 中运行此代码并在onDisappear 中撤消它以保持样式不同。

2) 使用 VStack / LazyVStack

【讨论】:

  • 感谢您的回复,很快就会给它!干杯:)
猜你喜欢
  • 2022-07-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-19
  • 1970-01-01
  • 2019-11-05
  • 2019-09-18
  • 2021-05-04
  • 2021-12-10
相关资源
最近更新 更多