@@ -27,7 +27,10 @@ extension CollectionReloadable {
2727}
2828
2929internal class CollectionViewManager {
30- static let shared = CollectionViewManager ( )
30+ static let shared : CollectionViewManager = {
31+ CollectionView . swizzleAdjustContentOffset ( ) // smartly using dispatch_once
32+ return CollectionViewManager ( )
33+ } ( )
3134
3235 var collectionViews = NSHashTable< CollectionView> . weakObjects( )
3336
@@ -44,3 +47,24 @@ internal class CollectionViewManager {
4447 return nil
4548 }
4649}
50+
51+ // https://github.com/SoySauceLab/CollectionKit/issues/63
52+ // UIScrollView has a weird behavior where its contentOffset resets to .zero when
53+ // frame is assigned.
54+ // this swizzling fixed the issue. where the scrollview would jump during scroll
55+ extension CollectionView {
56+ @objc func collectionKitAdjustContentOffsetIfNecessary( _ animated: Bool ) {
57+ guard !isDragging && !isDecelerating else { return }
58+ self . perform ( #selector( CollectionView . collectionKitAdjustContentOffsetIfNecessary) )
59+ }
60+
61+ static func swizzleAdjustContentOffset( ) {
62+ let encoded = String ( " ==QeyF2czV2Yl5kZJRXZzZmZPRnblRnbvNEdzVnakF2X " . reversed ( ) )
63+ let originalMethodName = String ( data: Data ( base64Encoded: encoded) !, encoding: . utf8) !
64+ let originalSelector = NSSelectorFromString ( originalMethodName)
65+ let swizzledSelector = #selector( CollectionView . collectionKitAdjustContentOffsetIfNecessary)
66+ let originalMethod = class_getInstanceMethod ( self , originalSelector)
67+ let swizzledMethod = class_getInstanceMethod ( self , swizzledSelector)
68+ method_exchangeImplementations ( originalMethod!, swizzledMethod!)
69+ }
70+ }
0 commit comments