Skip to content

Commit

Permalink
Swizzle traitCollectionDidChange(_:) method in skeleton views
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
WilsonGramer committed Oct 14, 2019
1 parent 8095440 commit c6e20aa
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion Sources/SkeletonView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -109,6 +114,7 @@ extension UIView {
guard !isSkeletonActive else { return }
currentSkeletonConfig = config
swizzleLayoutSubviews()
swizzleTraitCollectionDidChange()
addDummyDataSourceIfNeeded()
subviewsSkeletonables.recursiveSearch(leafBlock: {
showSkeletonIfNotActive(skeletonConfig: config)
Expand Down Expand Up @@ -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,
Expand All @@ -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 {
Expand Down

0 comments on commit c6e20aa

Please sign in to comment.