目录
目录README.md

Perseus Dark Mode

Actions Status Version Pod Platforms Swift 4.2 License

Integration Capabilities

Standalone File Carthage compatible CocoaPods manager Swift Package Manager compatible

Demo Apps and Others

Demo App Demo App PerseusUISystemKit XcodeTemplateProject

In Brief

This library lets a developer being awared of Dark Mode via a variable DarkMode.style. Also, with this library it is possible to change the value of Dark Mode in runtime easily.

Reqirements

  • Xcode 10.1+
  • Swift 4.2+
  • iOS: 9.3+, UIKit SDK
  • macOS: 10.9+, AppKit SDK

Third-party software

Installation

Step 1: Add PerseusDarkMode to a host project tree

Standalone

Make a copy of the file PerseusDarkModeSingle.swift then put it into a place required of a host project.

CocoaPods

Podfile should contain:

target "ProjectTarget" do
  use_frameworks!
  pod 'PerseusDarkMode', '1.1.3'
end

Carthage

Cartfile should contain:

github "perseusrealdeal/PerseusDarkMode" == 1.1.4

Some Carthage usage tips placed here.

Swift Package Manager

  • As a package dependency so Package.swift should contain the following statements:
dependencies: [
        .package(url: "https://github.com/perseusrealdeal/PerseusDarkMode.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 2: Make DarkMode ready for using

iOS

Override the following method of the first screen to let Perseus know that system’s appearance changed:

class MainViewController: UIViewController {
    override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
        super.traitCollectionDidChange(previousTraitCollection)

        if #available(iOS 13.0, *) {
            AppearanceService.processTraitCollectionDidChange(previousTraitCollection)
        }
    }
}

Also, if Dark Mode is released with Settings bundle put the statements into the app’s delegate:

extension AppDelegate: UIApplicationDelegate {

    func applicationDidBecomeActive(_ application: UIApplication) {

        // Update Dark Mode from Settings
        if let choice = isDarkModeSettingsChanged() {
            // Change Dark Mode value in Perseus Dark Mode library
            AppearanceService.DarkModeUserChoice = choice
            // Update appearance in accoring with changed Dark Mode Style
            AppearanceService.makeUp()
        }
    }
}

Used functions are distributed via standalone file DarkModeSwitching.swift.

iOS and macOS

Call the method AppearanceService.makeUp() with the app’s delegate if appearance changing is going to take place:

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()
    }
}

Copy the file DarkModeSwitching.swift into a host project for having fun with manual changing Dark Mode value.

Usage

Each time if Dark Mode changed the mentioned method #selector(makeUp) called, but registering is required:

class MainViewController: UIViewController {

    // At any view controller where changing is required

    override func viewDidLoad() {
        super.viewDidLoad()

        AppearanceService.register(stakeholder: self, selector: #selector(makeUp))
    }

    @objc private func makeUp() {
        print("^_^ \(AppearanceService.DarkModeUserChoice)"

        switch DarkMode.style {
        case .light:
            // make drawings for light mode
            break
        case .dark:
            // make drawings for dark mode
            break
        }
    }
}

There is another way to be notified of Dark Mode—KVO. To learn have a look at sample directly.

License MIT

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.

Have a look at LICENSE for details.

Author and Acknowledgments

PerseusDarkMode 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.

邀请码
    Gitlink(确实开源)
  • 加入我们
  • 官网邮箱:gitlink@ccf.org.cn
  • QQ群
  • QQ群
  • 公众号
  • 公众号

©Copyright 2023 CCF 开源发展委员会
Powered by Trustie& IntelliDE 京ICP备13000930号