From c6e20aa1a00f2793190e771967a8442a062f0b59 Mon Sep 17 00:00:00 2001 From: Wilson Gramer Date: Sun, 13 Oct 2019 20:47:03 -0400 Subject: [PATCH] Swizzle traitCollectionDidChange(_:) method in skeleton views Make skeleton views respond to changes in system appearance by updating the skeleton view whenever the trait collection changes, so the colors change automatically with dark mode. --- Sources/SkeletonView.swift | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/Sources/SkeletonView.swift b/Sources/SkeletonView.swift index 82cce2f6..faebe3f2 100755 --- a/Sources/SkeletonView.swift +++ b/Sources/SkeletonView.swift @@ -97,6 +97,11 @@ extension UIView { guard isSkeletonActive else { return } layoutSkeletonIfNeeded() } + + @objc func skeletonTraitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) { + guard isSkeletonActive, let config = currentSkeletonConfig else { return } + updateSkeleton(skeletonConfig: config) + } func showSkeleton(skeletonConfig config: SkeletonConfig) { isSkeletonAnimated = config.animated @@ -109,6 +114,7 @@ extension UIView { guard !isSkeletonActive else { return } currentSkeletonConfig = config swizzleLayoutSubviews() + swizzleTraitCollectionDidChange() addDummyDataSourceIfNeeded() subviewsSkeletonables.recursiveSearch(leafBlock: { showSkeletonIfNotActive(skeletonConfig: config) @@ -208,7 +214,7 @@ extension UIView { private func swizzleLayoutSubviews() { DispatchQueue.main.asyncAfter(deadline: .now() + 0.01) { - DispatchQueue.once(token: "UIView.SkeletonView.swizzle") { + DispatchQueue.once(token: "UIView.SkeletonView.swizzleLayoutSubviews") { swizzle(selector: #selector(UIView.layoutSubviews), with: #selector(UIView.skeletonLayoutSubviews), inClass: UIView.self, @@ -217,6 +223,17 @@ extension UIView { } } } + + private func swizzleTraitCollectionDidChange() { + DispatchQueue.main.asyncAfter(deadline: .now() + 0.01) { + DispatchQueue.once(token: "UIView.SkeletonView.swizzleTraitCollectionDidChange") { + swizzle(selector: #selector(UIView.traitCollectionDidChange(_:)), + with: #selector(UIView.skeletonTraitCollectionDidChange(_:)), + inClass: UIView.self, + usingClass: UIView.self) + } + } + } } extension UIView {