Currently, I have a single row horizontal UICollectionView
with UICollectionViewCompositionalLayout
[All] [Calendar] [Home2] [Work3] [Work4] ... [Work8] [⚙]
The UICollectionView
, is acting as a scrollable Tab layout navigation bar.
When user switch to [⚙]
page, he is able to delete the tab before itself. In this case, during the 1st time of the delete operation, tab [Work8]
will be removed.
Here's the straightforward code to achieve so.
https://github.com/yccheok/ios-tutorial/blob/debug/TabDemo/TabDemo/ViewController.swift#L144
func debug() {
if tabInfos.count < 2 {
return
}
let index = tabInfos.count-2
tabInfos.remove(at: index)
let indexPath = getIndexPath(index)
self.tabCollectionView.deleteItems(at: [indexPath])
// TODO: Possible solution.
/*
self.tabCollectionView.reloadData()
DispatchQueue.main.async() {
let indexPath = self.getIndexPath(self.tabInfos.count-1)
self.tabCollectionView.selectItem(at: indexPath, animated: false, scrollPosition: .centeredVertically)
self.tabCollectionView.scrollToItem(at: indexPath, at: .right, animated: true)
}
*/
// Clear left/ right cached view controllers - https://stackoverflow.com/a/21624169/72437
pageViewController.dataSource = nil
pageViewController.dataSource = self
// Don't forget to adjust the selection index.
if index < self.selectedTabInfoIndex {
selectedTabInfoIndex -= 1
}
}
Strange outcome
https://www.youtube.com/watch?v=Wzgb4QErUes (Please refer to this video for complete deletion animation)
This is how it looks like before deletion
As you can see in the video, after the deletion of [Work8]
, the left side tab ([Work3]
) is not visible
My expected outcome is, [Work3]
should be visible too immediately, without any additional action from user. I need to tap on the UICollectionView
, move it abit, only all the tabs will appear
I prefer not to use reloadData
, as
- It will not preserve scroll position of
UICollectionView
. - It does not have animation.
Do you have any idea what is the root cause of this strange animation outcome?
Here's the complete source code to try it out - https://github.com/yccheok/ios-tutorial/tree/debug (Folder TabDemo)