Skip to content

Commit 8a5f5b1

Browse files
authored
fix content offset being reseted when frame is assigned (#69)
* fix content offset being reseted when frame is assign * bump version * make swizzling more safe
1 parent 54f1d95 commit 8a5f5b1

3 files changed

Lines changed: 27 additions & 3 deletions

File tree

CollectionKit.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Pod::Spec.new do |s|
22
s.name = "CollectionKit"
3-
s.version = "1.3.0"
3+
s.version = "1.3.1"
44
s.summary = "A modern swift framework for building data-driven reusable collection view components."
55

66
s.description = <<-DESC

Sources/Info.plist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
<key>CFBundlePackageType</key>
1616
<string>FMWK</string>
1717
<key>CFBundleShortVersionString</key>
18-
<string>1.3.0</string>
18+
<string>1.3.1</string>
1919
<key>CFBundleVersion</key>
2020
<string>$(CURRENT_PROJECT_VERSION)</string>
2121
<key>NSPrincipalClass</key>

Sources/Other/CollectionReloadable.swift

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@ extension CollectionReloadable {
2727
}
2828

2929
internal 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

Comments
 (0)