Maintain scroll position in UICollectionView when scrolling up
As I commented earlier, it may be better to get the delta
from contentSize
rather than origin of a specific cell. A suggestion based on your own version:
let previousContentHeight = collectionView.contentSize.height
adapter.performUpdates(animated: false) { [weak self] completed in
guard let strongSelf = self else { return }
let delta = strongSelf.collectionView.contentSize.height - previousContentHeight
strongSelf.collectionView.bounds.origin.y += delta
}
UICollectionView auto scroll to cell at IndexPath
I've found that scrolling in viewWillAppear
may not work reliably because the collection view hasn't finished it's layout yet; you may scroll to the wrong item.
I've also found that scrolling in viewDidAppear
will cause a momentary flash of the unscrolled view to be visible.
And, if you scroll every time through viewDidLayoutSubviews
, the user won't be able to manually scroll because some collection layouts cause subview layout every time you scroll.
Here's what I found works reliably:
Objective C :
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
// If we haven't done the initial scroll, do it once.
if (!self.initialScrollDone) {
self.initialScrollDone = YES;
[self.collectionView scrollToItemAtIndexPath:self.myInitialIndexPath
atScrollPosition:UICollectionViewScrollPositionRight animated:NO];
}
}
Swift :
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
if !self.initialScrollDone {
self.initialScrollDone = true
self.testNameCollectionView.scrollToItem(at:selectedIndexPath, at: .centeredHorizontally, animated: true)
}
}
UICollectionView insertItemsAtIndexPaths: seems to reload collectionview or autoscrolls to top
I experimented the same problem, by calling "insertItemAtIndexPaths" to increment data into my collectionView.
I noticed two things :
- If you add items AFTER the current visible index path, there's no automatic scroll
- If you add items BEFORE the current visible index path, there's no automatic scroll, like you said, but the contentOffset is kept at the same position, but with different contentSize, because you've just added new items.
To deal with this "side effect", I used the solution mentioned here : UICollectionView insert cells above maintaining position (like Messages.app)
As it's explained in the following post, the "CATransaction.setDisableAction(false)" is the key thing to update smoothly the contentOffset position.
UICollectionView container padding
Please try to put following code inside viewDidLoad()
:
collectionView!.contentInset = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
This will add padding to every side of the collectionView.
PS: I know the question is here for a while but it might help somebody ;)
Related Topics
Mkmapview Show Incorrectly Saved Region
Wkwebview Function for Detecting If the Url Has Changed
iOS Automatically Add Hyphen in Text Field
Replace Occurrences of Nsnull in Nested Nsdictionary
Confused About Orthographic Projection of Camera in Scenekit
"No Such Module 'Alamofire'" Xcode Won't Recognize Alamofire Framework
Delivery Report of Sending Remote Push Notifications Using Apns - iOS
Xcode 6 Gm - Cllocationmanager
How to Express Strings in Swift Using Unicode Hexadecimal Values (Utf-16)
Firebase Limit on the Number of Observers/Listeners
iOS 8 Uiactivityviewcontroller and Uialertcontroller Button Text Color Uses Window's Tintcolor
Union Uibezierpaths Rather Than Apend Path
How to Convert an Uiimage to Grayscale in Swift Using Cifilter
Wrap Items in a Horizontal Uistackview on Multiple Lines
How to Implement Login/Logout Navigation Using Userdefaults in Swift