[英]How to make sticky bar in SwiftUI?
I'm novice in SwiftUI and still can't understand how to make sticky bar on the top of the List
.我是SwiftUI 的新手,仍然无法理解如何在
List
顶部制作粘性栏。 Like letters in apple music app when you listing artists or songs (look example ).当您列出艺术家或歌曲时,就像苹果音乐应用程序中的字母一样(例如)。
I explored abilities of the List
and NavigationView
, but got nothing.我探索了
List
和NavigationView
能力,但一无所获。 😔 😔
SwiftUI's
list view has built-in support for sections and section headers, just like UITableView
in UIKit
. SwiftUI's
列表视图内置了对部分和部分标题的支持,就像UIKit
UITableView
一样。 To add a section around some cells, start by placing a Section
around it.要在某些单元格周围添加一个部分,
Section
在它周围放置一个Section
。
What we want to do is create a list view that has two sections: one for important tasks and one for less important tasks.我们想要做的是创建一个列表视图,它有两个部分:一个用于重要任务,一个用于不太重要的任务。 Here's how that looks:
这是它的外观:
struct ContentView: View {
var body: some View {
List {
Section(header: Text("Important tasks")) {
TaskRow()
TaskRow()
TaskRow()
}
Section(header: Text("Other tasks")) {
TaskRow()
TaskRow()
TaskRow()
}
}
}
}
LazyVStack
提供了一个pinnedView
属性来完成这个。
Use pinnedViews
in the LazyVStack
initializer.在
LazyVStack
初始值pinnedViews
中使用pinnedViews
。
LazyVStack(pinnedViews: [.sectionHeaders]) {
Section(header: Text("Sticky Header")) {
ForEach(0...3) { item in
Text(item)
}
}
}
You can use List style .listStyle(PlainListStyle())
on List and use Section
on iOS 13 +您可以在 List 上使用 List 样式
.listStyle(PlainListStyle())
并在 iOS 13 + 上使用Section
for Example例如
struct ContentView: View {
var content = MyData()
var body: some View {
List {
ForEach(content.sections, id: \.title) { section in
Section(content: {
ForEach(section.rows, id: \.self) { row in
Text(row)
}
}, header: {
Text(section.title)
})
}
}
.listStyle(PlainListStyle())
}
}
Given:鉴于:
struct MyData {
struct Section {
var title: String
var rows: [String]
}
var sections: [Section] = []
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.