Disable scroll indicator in all tableview / collectionview

Viewed 35

If I want to disable the scroll indicator in all tableview and collectionview, what minimum changes do I need to make?

Suppose, I have 50 tableviews in my project, I don't want to set the property below in all of them or make changes to so many storyboards.

self.tableView.showsHorizontalScrollIndicator = false
self.tableView.showsVerticalScrollIndicator = false

Basically, I need to mutate a property of an object in one place only that will be reflected in all its instances or child inheriting it.

More context:

  • I tried protocol with default implementation but I need to call it somewhere anyway. Is there a hook that gets called every time an object in swift is instantiated for a particular class where I can attach some function or execution block that mutates those properties?
  • Maybe javascript object prototype mutation is something similar to what I am looking for, which has an adverse performance impact at the time of mutation, and in subsequent execution. What is the performance penalty for similar things (if exist) in swift?
2 Answers

Use the following snippet in AppDelegate or SceneDelegate depending on what you have:

 UITableView.appearance().showsVerticalScrollIndicator = false
 UITableView.appearance().showsHorizontalScrollIndicator = false

You can subclass a UITableView and use that class everywhere instead of default one.

class MyTableView: UITableView {

    override init(frame: CGRect, style: UITableView.Style) {
        super.init(frame: frame, style: style)
        
        self.showsVerticalScrollIndicator = false
        self.showsHorizontalScrollIndicator = false
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        
        self.showsVerticalScrollIndicator = false
        self.showsHorizontalScrollIndicator = false
    }
}

Now use this class everywhere. If you create tableview in storyboard, set its class to MyTableView.

Related