I have an application that uses more UIScrollViews
and extensions
from it like UICollectionViews
and UITableViews
and for a little dynamism I have created some general functions to help me with some paddings / margins.
My functions are created as extensions
for UIScrollView
and I want to use them just when the scroll view is only vertical
.
My Question
How can I find the scrolling direction of a UIScrollView?
I have found a lot of answers of how to find the scrolling direction, but I need when the UIScrollView
is initialised to find the scrolling direction of it, if it’s vertical or horizontal
.
Thank you for your time!
2
Answers
One solution I can think of in order to get the scroll direction before scrolling is to react accordingly to the type of UIScrollView in question.
So first step check if it is a UIScrollView, UICollectionView or UITableView.
In the case of a UITableView
I think a fair assumption here is that the scrolling direction will be vertical
In the case of a UIScrollView
This one could be tricky but I guess here you would have to compare the content size of the scroll view with the frame of the scrollview as Desdenova suggested and this can suggest if you can scroll horizontally and / or vertically.
In the case of a UICollectionView
This data will lie in the in the layout of the UICollectionView.
For example, I set up a basic UICollectionView in Storyboard using a FlowLayout without any data in it.
Then I add this code somewhere appropriate
This prints out
horizontal
.For the UIScrollView you have to use the UIScrollViewDelegate
scrollViewDidScroll
which is helps you in UIScrollView’s Scroll direction also for that you have to add some checks for the get the directions: