Overriding delegate property of UIScrollView in Swift (like UICollectionView does)

I think overriding an inherited property is something that’s possible in Objective-C but not (at least currently) in Swift. The way I’ve handled this is to declare a separate delegate as a computed property of the correct type that gets and sets the actual delegate: @objc protocol MyScrollViewDelegate : UIScrollViewDelegate, NSObjectProtocol { func myHeight() -> … Read more

How to implement horizontally infinite scrolling UICollectionView?

If your data is static and you want a kind of circular behavior, you can do something like this: var dataSource = [“item 0”, “item 1”, “item 2”] func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return Int.max // instead of returnin dataSource.count } func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { let … Read more

UIScrollView, reaching the bottom of the scroll view

I think what you might be able to do is to check that your contentOffset point is at the bottom of contentSize. So you could probably do something like: – (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { float bottomEdge = scrollView.contentOffset.y + scrollView.frame.size.height; if (bottomEdge >= scrollView.contentSize.height) { // we are at the end } } You’ll likely also … Read more

Setting contentOffset programmatically triggers scrollViewDidScroll

It is possible to change the content offset of a UIScrollView without triggering the delegate callback scrollViewDidScroll:, by setting the bounds of the UIScrollView with the origin set to the desired content offset. CGRect scrollBounds = scrollView.bounds; scrollBounds.origin = desiredContentOffset; scrollView.bounds = scrollBounds;

Detecting UIScrollView page change

Use this to detect which page is currently being shown and perform some action on page change: – (void)scrollViewDidScroll:(UIScrollView *)scrollView { static NSInteger previousPage = 0; CGFloat pageWidth = scrollView.frame.size.width; float fractionalPage = scrollView.contentOffset.x / pageWidth; NSInteger page = lround(fractionalPage); if (previousPage != page) { // Page has changed, do your thing! // … // … Read more

Check if a UIScrollView reached the top or bottom

Implement the UIScrollViewDelegate in your class, and then add this: -(void)scrollViewDidScroll: (UIScrollView*)scrollView { float scrollViewHeight = scrollView.frame.size.height; float scrollContentSizeHeight = scrollView.contentSize.height; float scrollOffset = scrollView.contentOffset.y; if (scrollOffset == 0) { // then we are at the top } else if (scrollOffset + scrollViewHeight == scrollContentSizeHeight) { // then we are at the end } } … Read more