Reputation: 4491
I wanted to show the UIScrollView's scrollbar only when needed, i.e. when the content exceeds the visible size and needs scrolling to see everything.
So I added this code:
self.view.addSubview(self.myScrollView)
self.myScrollView.edgesToSuperView()
print(self.myScrollView.contentSize.height)
print(self.myScrollView.bounds.height)
if self.myScrollView.contentSize.height > self.myScrollView.bounds.height {
self.myScrollView.showsVerticalScrollIndicator = true
}
However, contentSize.height and contentSize.height always print out as zero, although that inside the scrollView there is a stackView that has many text labels that exceeded screen height. So this didn't worked out.
Any better ideas?
Upvotes: 1
Views: 1415
Reputation: 77432
This property:
.showsVerticalScrollIndicator = true
only controls the visibility of a scroll indicator while the user is scrolling.
By default, if your content is not "tall enough" to need to scroll, the scroll indicators are never shown.
If your content IS "tall enough" to need to scroll (height is greater than the scroll view's frame height), the scroll indicators are shown while scrolling.
There is no way to have them "always showing."
If you want to indicate to the user that there is more content to be seen by scrolling, you could add something like "arrows" that you show / hide as appropriate, or, you can call `self.myScrollView.flashScrollIndicators() to "flash" the scroll indicators.
Upvotes: 2