From 809544066f419560d28f35a47b9eeb2c0ba29870 Mon Sep 17 00:00:00 2001 From: Wilson Gramer Date: Sun, 13 Oct 2019 20:44:12 -0400 Subject: [PATCH 1/5] Make skeleton colors dynamic If on iOS 13 or higher, returns a UIColor using the `init(dynamicProvider:)` initializer so it automatically adjusts to dark mode. Also switches out the `clouds` default color with the dynamic `skeletonDefault` color. --- Sources/Appearance/SkeletonAppearance.swift | 4 ++-- Sources/Extensions/UIColor+Skeleton.swift | 22 ++++++++++++++++++++- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/Sources/Appearance/SkeletonAppearance.swift b/Sources/Appearance/SkeletonAppearance.swift index e3e2efb2..40d6ef51 100644 --- a/Sources/Appearance/SkeletonAppearance.swift +++ b/Sources/Appearance/SkeletonAppearance.swift @@ -19,9 +19,9 @@ public enum SkeletonAppearance { class SkeletonViewAppearance: Appearance { static var shared = SkeletonViewAppearance() - var tintColor: UIColor = .clouds + var tintColor: UIColor = .skeletonDefault - var gradient: SkeletonGradient = SkeletonGradient(baseColor: .clouds) + var gradient: SkeletonGradient = SkeletonGradient(baseColor: .skeletonDefault) var multilineHeight: CGFloat = 15 diff --git a/Sources/Extensions/UIColor+Skeleton.swift b/Sources/Extensions/UIColor+Skeleton.swift index 515c0234..2f2f6b44 100644 --- a/Sources/Extensions/UIColor+Skeleton.swift +++ b/Sources/Extensions/UIColor+Skeleton.swift @@ -21,7 +21,13 @@ extension UIColor { } public var complementaryColor: UIColor { - return isLight() ? darker : lighter + if #available(iOS 13, *) { + return UIColor { traitCollection in + return self.isLight() ? self.darker : self.lighter + } + } else { + return isLight() ? darker : lighter + } } public var lighter: UIColor { @@ -58,11 +64,25 @@ public extension UIColor { static var carrot = UIColor(0xe67e22) static var alizarin = UIColor(0xe74c3c) static var clouds = UIColor(0xecf0f1) + static var darkClouds = UIColor(0x1c2325) static var concrete = UIColor(0x95a5a6) static var flatOrange = UIColor(0xf39c12) static var pumpkin = UIColor(0xd35400) static var pomegranate = UIColor(0xc0392b) static var silver = UIColor(0xbdc3c7) static var asbestos = UIColor(0x7f8c8d) + + static var skeletonDefault: UIColor { + if #available(iOS 13, *) { + return UIColor { traitCollection in + switch traitCollection.userInterfaceStyle { + case .dark: return .darkClouds + default: return .clouds + } + } + } else { + return .clouds + } + } } // codebeat:enable[TOO_MANY_IVARS] From c6e20aa1a00f2793190e771967a8442a062f0b59 Mon Sep 17 00:00:00 2001 From: Wilson Gramer Date: Sun, 13 Oct 2019 20:47:03 -0400 Subject: [PATCH 2/5] 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 { From f6d4343cc66260c0e0151f80071e6798dd208dad Mon Sep 17 00:00:00 2001 From: Wilson Gramer Date: Sun, 13 Oct 2019 20:55:53 -0400 Subject: [PATCH 3/5] Update example to support dark mode --- Example/TableView/Base.lproj/Main.storyboard | 32 ++++++++++---------- Example/TableView/Constants.swift | 2 +- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/Example/TableView/Base.lproj/Main.storyboard b/Example/TableView/Base.lproj/Main.storyboard index 33974e1e..3e5ad92c 100644 --- a/Example/TableView/Base.lproj/Main.storyboard +++ b/Example/TableView/Base.lproj/Main.storyboard @@ -1,9 +1,9 @@ - - + + - + @@ -13,19 +13,19 @@ - + - + - Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. + @@ -50,7 +50,7 @@ - + @@ -65,7 +65,7 @@ - + @@ -88,7 +88,7 @@ - + @@ -159,7 +159,7 @@ - + @@ -227,7 +227,7 @@ - + @@ -267,9 +267,9 @@ - + - + diff --git a/Example/TableView/Constants.swift b/Example/TableView/Constants.swift index cb6e0f29..066eea35 100644 --- a/Example/TableView/Constants.swift +++ b/Example/TableView/Constants.swift @@ -2,4 +2,4 @@ import UIKit -let colors = [(UIColor.turquoise,"turquoise"), (UIColor.emerald,"emerald"), (UIColor.peterRiver,"peterRiver"), (UIColor.amethyst,"amethyst"),(UIColor.wetAsphalt,"wetAsphalt"), (UIColor.nephritis,"nephritis"), (UIColor.belizeHole,"belizeHole"), (UIColor.wisteria,"wisteria"), (UIColor.midnightBlue,"midnightBlue"), (UIColor.sunFlower,"sunFlower"), (UIColor.carrot,"carrot"), (UIColor.alizarin,"alizarin"),(UIColor.clouds,"clouds"), (UIColor.concrete,"concrete"), (UIColor.flatOrange,"flatOrange"), (UIColor.pumpkin,"pumpkin"), (UIColor.pomegranate,"pomegranate"), (UIColor.silver,"silver"), (UIColor.asbestos,"asbestos")] +let colors = [(UIColor.skeletonDefault,"skeletonDefault"),(UIColor.turquoise,"turquoise"), (UIColor.emerald,"emerald"), (UIColor.peterRiver,"peterRiver"), (UIColor.amethyst,"amethyst"),(UIColor.wetAsphalt,"wetAsphalt"), (UIColor.nephritis,"nephritis"), (UIColor.belizeHole,"belizeHole"), (UIColor.wisteria,"wisteria"), (UIColor.midnightBlue,"midnightBlue"), (UIColor.sunFlower,"sunFlower"), (UIColor.carrot,"carrot"), (UIColor.alizarin,"alizarin"),(UIColor.clouds,"clouds"), (UIColor.concrete,"concrete"), (UIColor.flatOrange,"flatOrange"), (UIColor.pumpkin,"pumpkin"), (UIColor.pomegranate,"pomegranate"), (UIColor.silver,"silver"), (UIColor.asbestos,"asbestos")] From da51a6f673b94843f934330b0d4c6da05a6c4bfc Mon Sep 17 00:00:00 2001 From: Wilson Gramer Date: Sun, 13 Oct 2019 21:04:45 -0400 Subject: [PATCH 4/5] Update English README --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index c391c79d..3e9a311c 100755 --- a/README.md +++ b/README.md @@ -326,9 +326,9 @@ Besides, ```SkeletonView``` features 20 flat colors 🤙🏼 Default values: - **tintColor**: UIColor - - *default: .clouds* + - *default: `.skeletonDefault` (same as `.clouds` but adaptive to dark mode)* - **gradient**: SkeletonGradient - - *default: SkeletonGradient(baseColor: .clouds)* + - *default: `SkeletonGradient(baseColor: .skeletonDefault)`* - **multilineHeight**: CGFloat - *default: 15* - **multilineSpacing**: CGFloat From 264b0a70c28c6905a6a1f223c85841b1d73c89b1 Mon Sep 17 00:00:00 2001 From: Wilson Gramer Date: Tue, 22 Oct 2019 12:51:26 -0400 Subject: [PATCH 5/5] Update CHANGELOG --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index d3f51b87..4d374496 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,10 @@ All notable changes to this project will be documented in this file ## Next version +### New + +- Support for iOS 13 dark mode. (thanks @Wilsonator5000) + ### Improvements - Fix completion call in .none transition style while hide skeletons. (thanks @aadudyrev)