Skip to content

Commit

Permalink
UnSwizzle methods when SkeletonView is hidden (#381)
Browse files Browse the repository at this point in the history
* Add InputAccessoryView to textField

* Add method to removeOnce Add unSwizzleMethods that are called when recursiveHideSkeleton

Co-authored-by: Alexandros Lykesas <[email protected]>
  • Loading branch information
alexookah and Alexandros Lykesas authored Apr 8, 2021
1 parent 4143a60 commit 61a6efb
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 7 deletions.
5 changes: 3 additions & 2 deletions Example/TableView/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="17506" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="Va7-1y-Tel">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="17701" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="Va7-1y-Tel">
<device id="retina5_9" orientation="portrait" appearance="light"/>
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="17505"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="17703"/>
<capability name="Safe area layout guides" minToolsVersion="9.0"/>
<capability name="System colors in document resources" minToolsVersion="11.0"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
Expand Down Expand Up @@ -155,6 +155,7 @@
<connections>
<outlet property="avatar" destination="oiE-tt-nc2" id="Dkh-R5-Qhu"/>
<outlet property="label1" destination="VhU-1t-AaI" id="kUW-HV-KrD"/>
<outlet property="textField" destination="dha-bH-Ipf" id="OHI-6P-tuU"/>
</connections>
</tableViewCell>
</prototypes>
Expand Down
18 changes: 16 additions & 2 deletions Example/TableView/Cell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,25 @@
import UIKit

class Cell: UITableViewCell {

@IBOutlet weak var avatar: UIImageView!
@IBOutlet weak var label1: UILabel!

@IBOutlet weak var textField: UITextField!

override func awakeFromNib() {
super.awakeFromNib()
setUpInputAccessoryView()
}

func setUpInputAccessoryView() {
let bar = UIToolbar()
let reset = UIBarButtonItem(title: "InputAccessoryView", style: .plain, target: self, action: #selector(resetTapped))
bar.items = [reset]
bar.sizeToFit()
textField.inputAccessoryView = bar
}

@objc func resetTapped() {

}
}
13 changes: 10 additions & 3 deletions Sources/Helpers/Swizzling.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,28 @@ import Foundation

extension DispatchQueue {
private static var _onceTracker = [String]()

class func once(token: String, block: () -> Void) {
objc_sync_enter(self); defer { objc_sync_exit(self) }
guard !_onceTracker.contains(token) else { return }

_onceTracker.append(token)
block()
}

class func removeOnce(token: String, block: () -> Void) {
objc_sync_enter(self); defer { objc_sync_exit(self) }
guard let index = _onceTracker.firstIndex(of: token) else { return }
_onceTracker.remove(at: index)
block()
}
}

func swizzle(selector originalSelector: Selector, with swizzledSelector: Selector, inClass: AnyClass, usingClass: AnyClass) {
guard let originalMethod = class_getInstanceMethod(inClass, originalSelector),
let swizzledMethod = class_getInstanceMethod(usingClass, swizzledSelector)
else { return }

if class_addMethod(inClass, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod)) {
class_replaceMethod(inClass, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod))
} else {
Expand Down
24 changes: 24 additions & 0 deletions Sources/SkeletonView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,8 @@ extension UIView {
isHidden = false
}
currentSkeletonConfig?.transition = transition
unSwizzleLayoutSubviews()
unSwizzleTraitCollectionDidChange()
removeDummyDataSourceIfNeeded(reloadAfter: reload)
subviewsSkeletonables.recursiveSearch(leafBlock: {
recoverViewState(forced: false)
Expand Down Expand Up @@ -233,6 +235,17 @@ extension UIView {
}
}

private func unSwizzleLayoutSubviews() {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.01) {
DispatchQueue.removeOnce(token: "UIView.SkeletonView.swizzleLayoutSubviews") {
swizzle(selector: #selector(UIView.skeletonLayoutSubviews),
with: #selector(UIView.layoutSubviews),
inClass: UIView.self,
usingClass: UIView.self)
}
}
}

private func swizzleTraitCollectionDidChange() {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.01) {
DispatchQueue.once(token: "UIView.SkeletonView.swizzleTraitCollectionDidChange") {
Expand All @@ -243,6 +256,17 @@ extension UIView {
}
}
}

private func unSwizzleTraitCollectionDidChange() {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.01) {
DispatchQueue.removeOnce(token: "UIView.SkeletonView.swizzleTraitCollectionDidChange") {
swizzle(selector: #selector(UIView.skeletonTraitCollectionDidChange(_:)),
with: #selector(UIView.traitCollectionDidChange(_:)),
inClass: UIView.self,
usingClass: UIView.self)
}
}
}
}

extension UIView {
Expand Down

0 comments on commit 61a6efb

Please sign in to comment.