This library brings a developer some modern but simple tools like system and semantic colors, and dynamic image. Features of this library can be used to create Dark Mode sensitive UI for both macOS and iOS apps as well.
- Xcode 10.1+
- Swift 4.2+
- iOS: 9.3+, UIKit SDK
- macOS: 10.10+, AppKit SDK
- SwiftLint Shell Script Runner
- SwiftLint / 0.31.0: Busy Laundromat for macOS High Sierra
Using "Exact" with the Version field is strongly recommended.
Make a copy of the file PerseusUISystemKitSingle.swift
then put it into a place required of a host project.
Cartfile should contain:
github "perseusrealdeal/PerseusDarkMode" == 1.1.5
github "perseusrealdeal/PerseusUISystemKit" == 1.1.4
Some Carthage usage tips placed here.
Podfile should contain:
target "ProjectTarget" do
use_frameworks!
pod 'PerseusUISystemKit', '1.1.3'
end
- As a package dependency Package.swift should contain the following statements:
dependencies: [
.package(url: "https://github.com/perseusrealdeal/PerseusUISystemKit.git",
.exact("1.1.4"))
],
- As an Xcode project dependency:
Project in the Navigator > Package Dependencies > Add Package Dependency
Using "Exact" with the Version field is strongly recommended.
Step 1: import PerseusDarkMode and call makeUp() in app's delegate:
import PerseusDarkMode
extension AppDelegate: UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions
launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// ... code
// Call AppearanceService.makeUp() method if AppearanceService.register(:, :)
// is taken into account
AppearanceService.makeUp()
// ... otherwise call AppearanceService.recalculateStyleIfNeeded()
// to load DarkMode.style from user defaults
// AppearanceService.recalculateStyleIfNeeded()
}
}
Step 2: only if iOS, override method traitCollectionDidChange of the first app's screen, once
import PerseusDarkMode
class MainViewController: UIViewController {
override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
super.traitCollectionDidChange(previousTraitCollection)
if #available(iOS 13.0, *) {
AppearanceService.processTraitCollectionDidChange(previousTraitCollection)
}
}
}
Step 3: register a view controller of a screen
import PerseusDarkMode
class MainViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
AppearanceService.register(stakeholder: self, selector: #selector(makeUp))
}
@objc private func makeUp() {
// UI customs put here
}
}
Step 4: import PerseusUISystemKit and put all UI customs into makeUp() method
import PerseusDarkMode
import PerseusUISystemKit
class MainViewController: UIViewController {
// ... code
@objc private func makeUp() {
print("^_^ \(DarkMode.style) \(UIColor.perseusBlue.RGBA255)")
view.backgroundColor = .perseusBlue
}
}
There is another way to be notified of Dark Mode—KVO.
DarkModeImageView
class is an expressive sample of Dark Mode KVO usage for both macOS and iOS as well.
Copyright © 7530 - 7531 Mikhail Zhigulin of Novosibirsk.
- The year starts from the creation of the world according to a Slavic calendar.
- September, the 1st of Slavic year.
LICENSE for details.
PerseusUISystemKit
was written at Novosibirsk by Mikhail Zhigulin i.e. me, mzhigulin@gmail.com.
Mostly I'd like thank my lovely parents for supporting me in all my ways.