Start UICollectionView at a specific indexpath
Solution 1:
So I solved this a different way, using the UICollectionViewDelegate
method and a one-off Bool
:
Swift 2:
var onceOnly = false
internal func collectionView(collectionView: UICollectionView, willDisplayCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
if !onceOnly {
let indexToScrollTo = NSIndexPath(forRow: row, inSection: section)
self.problemListCollectionView.scrollToItemAtIndexPath(indexToScrollTo, atScrollPosition: .Left, animated: false)
onceOnly = true
}
}
Swift 3:
var onceOnly = false
internal func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
if !onceOnly {
let indexToScrollTo = IndexPath(item: row, section: section)
self.problemListCollectionView.scrollToItem(at: indexToScrollTo, at: .left, animated: false)
onceOnly = true
}
}
This code is executed before any animation occurs (so it really loads to this point), which is better than attempting to call in viewDidAppear
, and I didn't have success with it in viewWillAppear
.
Solution 2:
To solve this problem I partially used the greenhouse answer.
/// Edit
var startIndex: Int! = 0
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
collectionView.setNeedsLayout()
collectionView.layoutIfNeeded()
collectionView.scrollToItemAtIndexPath(
NSIndexPath(forItem: 0, inSection: startIndex),
atScrollPosition: .None,
animated: false)
}
The problem seems to be in the wrong collectionView
size. After setting the layout scrollToItemAtIndexPath
produces the needed result.
It also seems that this problem only persists when a Collection View
is used inside a UIViewController
.