I'm novice in SwiftUI and still can't understand how to make sticky bar on the top of the 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. 😔
SwiftUI's
list view has built-in support for sections and section headers, just like UITableView
in UIKit
. To add a section around some cells, start by placing a Section
around it.
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: [.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 +
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] = []
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.