Skip to content

Instantly share code, notes, and snippets.

@TheSwiftyCoder
Created February 12, 2017 10:22
Show Gist options
  • Save TheSwiftyCoder/e13f6adb42e247fe83c3969887db41bd to your computer and use it in GitHub Desktop.
Save TheSwiftyCoder/e13f6adb42e247fe83c3969887db41bd to your computer and use it in GitHub Desktop.
Swift 3 UICollectionView navigation Buttons
@IBAction private func locationLeftButton(_ sender: UIButton) {
let visibleItems: NSArray = self.locationsCollectionView.indexPathsForVisibleItems as NSArray
let currentItem: IndexPath = visibleItems.object(at: 0) as! IndexPath
let prevItem: IndexPath = IndexPath(item: currentItem.item - 1, section: 0)
if prevItem.row != -1 {
self.locationsCollectionView.scrollToItem(at: prevItem, at: .right, animated: true)
}
}
@IBAction private func locationRightButton(_ sender: UIButton) {
let visibleItems: NSArray = self.locationsCollectionView.indexPathsForVisibleItems as NSArray
let currentItem: IndexPath = visibleItems.object(at: 0) as! IndexPath
let nextItem: IndexPath = IndexPath(item: currentItem.item + 1, section: 0)
// StoredLocationsData is your collection view cell array
if nextItem.row < storedLocationsData.count {
self.locationsCollectionView.scrollToItem(at: nextItem, at: .left, animated: true)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment