Detecting iOS Dark Mode Change

后端 未结 6 1727
悲哀的现实
悲哀的现实 2020-12-14 16:19

I read through the documentation regarding: https://developer.apple.com/documentation/appkit/supporting_dark_mode_in_your_interface

When the user chan

6条回答
  •  无人及你
    2020-12-14 16:51

    Swift 5:

    traitCollectionDidChange also gets called a few times. This is how I detect DarkMode runtime change and setColors() only once.

    override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
            super.traitCollectionDidChange(previousTraitCollection)
    
            guard UIApplication.shared.applicationState == .inactive else {
                return
            }
    
            setColors()
        }
    

    In setColors() func I update the colors. Detecting current colorScheme:

    extension UIViewController {
        var isDarkMode: Bool {
            if #available(iOS 13.0, *) {
                return self.traitCollection.userInterfaceStyle == .dark
            }
            else {
                return false
            }
        }
    
    }
    

    I have colors defined like this (for iOS < 13):

    enum ColorCompatibility {
        static var myOlderiOSCompatibleColorName: UIColor {
            if UIViewController().isDarkMode {
                return UIColor(red: 33, green: 35, blue: 37, alpha: 0.85)
            }
            else {
                return UIColor(hexString: "#F3F3F3", alpha: 0.85)
            }
        }
    }
    

    Example:

    private func setColors() {
      myView.backgroundColor = ColorCompatibility.myOlderiOSCompatibleColorName
    }
    

    Also you might need to call setColors in ViewDidLoad/Will/DidAppear depending on your case like this:

    viewDidLoad() {
    ...
    setColors()
    ...
    }
    

    For iOS11+ you could use "named Colors", defined in Assets and much easier to use in IB.

    Cheers

提交回复
热议问题