Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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. 😔

like image 685
cyber-bot Avatar asked Jan 29 '20 14:01

cyber-bot


People also ask

How do I add a header in SwiftUI?

How to add section header/footer in SwiftUI list. After you group your data into a section, you can add a header and footer to a particular section by specifying a header and footer argument to Section . Text("Apps and websites will use the first language in this list that they support.")


3 Answers

LazyVStack provides an initializer with a pinnedView parameter that does exactly this.

like image 92
qzhann Avatar answered Oct 11 '22 23:10

qzhann


Use pinnedViews in the LazyVStack initializer.

LazyVStack(pinnedViews: [.sectionHeaders]) {
    Section(header: Text("Sticky Header")) {
        ForEach(0...3) { item in
            Text(item)
        }
    }
}
like image 6
Lindemann Avatar answered Oct 11 '22 23:10

Lindemann


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()
            }
        }
    }
}
like image 5
Enea Dume Avatar answered Oct 12 '22 00:10

Enea Dume