Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to hide scroll indicators in a SwiftUI List?

I want to create a SwiftUI List, but not show scroll indicators. ScrollView offers showsIndicators to do this. How can it be done?

like image 661
retterdesapok Avatar asked Oct 10 '19 09:10

retterdesapok


People also ask

How do I hide the scroll bar in Swift UI?

By default, when you create ScrollView and put content inside you see a scrollbar during scrolling. During scroll you see scrollbar: If you don't want this bar you can set showsIndicators for ScrollView to false and scrollbar disappears.

How do I make a list scrollable in SwiftUI?

If you want to programmatically make SwiftUI's List move to show a specific row, you should embed it inside a ScrollViewReader . This provides a scrollTo() method on its proxy that can move to any row inside the list, just by providing its ID and optionally also an anchor.


2 Answers

Any Indicators (List, scrollView, etc.)

you can get rid of showing indicators for all Lists, but with an API of the UITableView. because SwiftUI List is using UITableView for iOS behind the scene:

struct ContentView: View {
    
    init() {
        UITableView.appearance().showsVerticalScrollIndicator = false
    }
    
    var body: some View {
        List(0...100, id: \.self) { item  in
            Text("hey")
        }
    }
}

Note that this will eliminate all TableViews and Lists indicators. You should make it visible again if you need to.


⚠️ Not Yet Important Note

Seems like Apple is removing appearance hacks (but not for this one yet). So you can use LazyVStack inside and ScrollView instead of List and use the available argument for hiding the indicators.

struct ContentView: View {
    var body: some View {
        ScrollView(.vertical, showsIndicators: false) { // <- This argument
            LazyVStack {
                ForEach(1...100, id: \.self) {
                    Text("\($0)").frame(height: 40)
                }
            }
        }
    }
}
like image 194
Mojtaba Hosseini Avatar answered Sep 20 '22 12:09

Mojtaba Hosseini


It is actually easy to accomplish this without any appearance work arounds in the answer accepted. You just have to use the ScrollView initializer and set the parameter showsIndicators to false.

ScrollView(.vertical, showsIndicators: false) {
     // ... your content for scrollView
}

Inside your ScrollView you could use LazyVStack, if you have a lot of subviews to scroll through. SwiftUI will then render very efficiently your subviews: "lazy" -> only if needed).

like image 44
LukeSideWalker Avatar answered Sep 20 '22 12:09

LukeSideWalker