I have a UICollectionView that scrolls both horizontally and vertically. I'm trying to center an item in the middle of the collection view. When I did this in Objective-C, I simply used the following code:
[theCollection scrollToItemAtIndexPath:thePath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally|UICollectionViewScrollPositionCenteredVertically animated:TRUE];
However, in switching everything to Swift, this doesn't seem to work. When I do this:
theCollection.scrollToItem(at: thePath, at: .centeredVertically|.centeredHorizontally , animated: true)
An error occurs ('|' is not a postfix unary operator).
If I try to do the scrollToItem twice, once with each direction, it only does the second one.
Any ideas on how to make this happen with Swift?
Thanks!
In most of the APIs imported into Swift, bit position based flags such as UICollectionViewScrollPositionCenteredHorizontally
is not Int
.
So, bitwise operator like |
would not be applicable.
UICollectionViewScrollPosition flags seems to be wrapped into a struct called UICollectionView.ScrollPosition
which conforms to OptionSet
protocol.
When you use OptionSet
constants, you need to write them in Set-like notations.
Please try something like this:
theCollection.scrollToItem(at: thePath, at: [.centeredVertically,.centeredHorizontally] , animated: true)