Reputation: 2736
I have a UICollectionView added to a UIView in an app I am working on. Displayed in the UICollectionView I have a set of images pulled from Core Data. The vertical scrolling works fine but the scrollbar does not display until the user touches the UICollectionView.
How can I make sure the scrollbar at the right is always visible so that an indication is given to the user that it is scrollable?
Upvotes: 11
Views: 12267
Reputation: 507
I flash the scrollbar a few times to highlight that it's there.
flashScrollIndicatorsMultipleTimes(count: 3)
private func flashScrollIndicatorsMultipleTimes(count: Int) {
guard count > 0 else { return }
DispatchQueue.main.asyncAfter(deadline: .now() + 1.5) {
self.collectionView.flashScrollIndicators()
self.flashScrollIndicatorsMultipleTimes(count: count - 1) //
}
}
This will flash the scroll indicators three times with a 1.5-second delay between each flash.
Upvotes: 0
Reputation: 4601
This didn't work for me on iOS 16, I had to create a subclass of UICollectionView and layoutSubviews. And then set the flag where you want the scrollbar flashed, like reloadData
class FlashScrollBarCollectionView: UICollectionView {
var flashScrollBar = false
override func layoutSubviews() {
super.layoutSubviews()
if flashScrollBar {
flashScrollIndicators()
flashScrollBar = false
}
}
override func reloadData() {
flashScrollBar = true
super.reloadData()
}
}
Upvotes: 0
Reputation: 40211
You can't make them always visible. Instead, you can flash them once, when the user is first presented with the collection view to notify them, that this view can be scrolled.
Since UICollectionView
is a subclass of UIScrollView
you can do this:
[myCollectionView flashScrollIndicators];
Check out the Settings app for instance. When you go to a settings list that is longer then the screen, the scroll indicator is flashed once.
Upvotes: 17