I have a horizontal UICollectionView
which works fine and scrolls. When I tap an item I update my data and call reloadData
. This works and the new data is displayed in the UICollectionView
.
The problem is the scroll position doesn't change and it is still viewing the last place. I want to reset the scrolling to the top (Or left in my case). How can I do this?
相关问题
- CALayer - backgroundColor flipped?
- Core Data lightweight migration crashes after App
- Core Data lightweight migration crashes after App
- How can I implement password recovery in an iPhone
- back button text does not change
相关文章
- 现在使用swift开发ios应用好还是swift?
- Could I create “Call” button in HTML 5 IPhone appl
- TCC __TCCAccessRequest_block_invoke
- xcode 4 garbage collection removed?
- Unable to process app at this time due to a genera
- How can I add media attachments to my push notific
- How do you detect key up / key down events from a
- “Storyboard.storyboard” could not be opened
You want
setContentOffset:
. It takes a CGPoint as and argument that you can set to what ever you want using CGPointMake, but if you wish to return to the very beginning of the collection, you can simply use CGPointZero.If you view controller contains safe area, code:
doesn't work, so you can use universal extension:
Sorry I couldn't comment at the time of this writing, but I was using a UINavigationController and
CGPointZero
itself didn't get me to the very top so I had to use the following instead.Hope this helps somebody in future. Cheers!
In Swift:
If you leave the view that houses the collection view, make a change in the 2nd view controller, and need the collection view to update upon returning:
In my situation the unwind is great because the viewDidLoad call will update the CoreData context, the reloadData call will make sure the collectionView updates to reflect the new CoreData context, and then the contentOffset will make sure the table sets back to the top.
I use this quite often in different parts of my app so I just extended UIScrollView so it can be used on any scroll view and scroll view subclass:
So whenever I need to reset the position:
To deal with an
UINavigationController
and a transparent navigation bar, I had to calculate the extra top offset to perfectly match the position of the first element.Swift 1.1 code:
Cheers!