I have a horizontal collection view and every cell has it's own width. I use the default UICollectionViewFlowLayout. When rearranging, cell inherits the width of the cell which it overlays during this movement, what I don't want. Any idea, how to avoid this behavior?
相关问题
- Core Data lightweight migration crashes after App
- How can I implement password recovery in an iPhone
- State preservation and restoration strategies with
- “Zero out” sensitive String data in Swift
- SwiftUI: UIImage (QRCode) does not load after call
相关文章
- 现在使用swift开发ios应用好还是swift?
- UITableView dragging distance with UIRefreshContro
- Using if let syntax in switch statement
- TCC __TCCAccessRequest_block_invoke
- Where does a host app handle NSExtensionContext#co
- Enum with associated value conforming to CaseItera
- Swift - hide pickerView after value selected
- Is there a Github markdown language identifier for
So, If you have the UICollectionView with cells of different sizes and you'd like to keep their sizes when moving using the standard UICollectionViewFlowLayout use this:
}
In the moveItemAt method of your controller don't forget to check that the pressure state is changed like:
I solved this by using
collectionView:targetIndexPathForMoveFromItemAtIndexPath:toProposedIndexPath:
where if a copy of my row/section data didn't exist, I'd create it, then update the copy as if the proposed move was an actual move. MycollectionView:layout:sizeForItemAtIndexPath:
would use the copy if it existed. I would set the copy to nil incollectionView:moveItemAtIndexPath:toIndexPath:
so that the view would go back to using the "original" row/section data for sizing. All sizes look correct before, during, and after moving as a result.As requested, here's my code:
I also updated
collectionView:layout:sizeForItemAtIndexPath:
to use _movingSections instead of _sections when _movingSections is non-nil. And incollectionView:moveItemAtIndexPath:toIndexPath:
I set _movingSections to nil. And that's it!I should add that I ended up abandoning this method since I have sections that can be wider than the device width, which works fine until you move something, and at that point UICollectionViewFlowLayout wants to evenly space the cells regardless of the widths you've provided. Instead, I moved to using a UIScrollView and now my code is much simpler, plus I'm no longer fighting with UICollectionViewFlowLayout. I considered rolling my own UICollectionViewLayout, but that did not make my code simpler.