Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Dark Mode Ins & Outs

Dark Mode Ins & Outs

Patrick McCarron

November 19, 2019
Tweet

More Decks by Patrick McCarron

Other Decks in Programming

Transcript

  1. Dark Mode Ins & Outs Ready your app for iOS

    13’s most popular feature Patrick McCarron M1 Finance @McCarron
  2. iOS 13 Adoption High • 50% of all devices as

    of October 15th, which was < 30 days. (Source: Apple) • Users will ask you for it!
  3. OLED Power Advantage • OLED screens on iPhone X, XS

    and 11 • Black is actually black on OLED • Can operate on the lowest brightness in a dark room • Looks better AND uses lower power (Image Source: 9to5Mac)
  4. Info.plist Disable For Whole App • Apple says: “Only a

    small subset of apps really should be dark all the time, and those are media-centric or content-creation apps“. • Can disable by setting UIUserInterfaceStyle in your Info.plist to Light • <key>UIUserInterfaceStyle</key> <string>Light</string>
  5. Disable Per Item // On a view view.overrideUserInterfaceStyle = .light

    // Or… on your window! window?.overrideUserInterfaceStyle = .light
  6. Disable Child Controller //Or a child view controller let forcedTraitCollection

    = UITraitCollection(userInterfaceStyle: .dark) setOverrideTraitCollection(forcedTraitCollection, forChild: vc)
  7. Notes On Battery Life • Pure black is NOT required.

    Dark gray is OKAY • White: ~1000mW • Pure Black: ~600mW • Dark Gray: ~610mW • (Source: XDA-Developers)
  8. Dynamic code for UIColors return UIColor { trait -> UIColor

    in if trait.userInterfaceStyle == .dark { return UIColor.black } return UIColor.white }
  9. Dynamic code for UIColors w/ Helper public extension UIColor {

    static func dynamic(light: UIColor, dark: UIColor) -> UIColor { if #available(iOS 13, *) { return UIColor { trait -> UIColor in return trait.userInterfaceStyle == .dark ? dark : light } } return light } @nonobjc static var m1Text01: UIColor { return dynamic(light: UIColor.lightGray, dark: UIColor.darkGray) } }
  10. Dynamic code for UIImage var image = UIImage(named: "yang") if

    #available(iOS 13, *), traitCollection.userInterfaceStyle == .dark { image = UIImage(named: "yin") } imageView.image = image
  11. Dynamic code for UIImage w/ Helper public extension UIImageView {

    func setImage(light: UIImage, dark: UIImage) { if #available(iOS 13, *), traitCollection.userInterfaceStyle == .dark { image = dark } else { image = light } } }
  12. When To Do Colors? • Not in init or viewDidLoad

    • Best practice: traitCollectionDidChange unless you do it somewhere else • Apple’s suggestions:
  13. Trait Collection Did Change Example func setupColors() { backgroundView.layer.borderColor =

    UIColor.m1_ui_03.cgColor } public override func traitCollectionDidChange(_ previous: UITraitCollection?) { super.traitCollectionDidChange(previous) if #available(iOS 13, *), traitCollection.hasDifferentColorAppearance(comparedTo: previous), traitCollection.userInterfaceStyle != previous?.userInterfaceStyle { setupColors() } }
  14. Simulator / Device Mode Setting(s) • Simulator Setting • Xcode

    Environment Override • Control Center (Device only)
  15. Specialized Build Idea public extension UIColor { private static var

    kColorPostFix: String { // For testing Dark Mode colors in Debug builds #if DEBUG return "_DEBUG" #else return "" #endif } @nonobjc static var m1BackgroundPrimary: UIColor { return UIColor(named: "BackgroundPrimary\(kColorPostFix)")! } }
  16. M1 Holdings Inc. provides a range of financial services through

    its opera\ng subsidiaries. M1 Finance LLC and M1 Spend LLC are wholly-owned subsidiaries of M1 Holdings Inc. and are separate but affiliated companies. M1 Finance LLC is a SEC registered broker-dealer and Member FINRA (www.finra.org) / SIPC (www.sipc.org). Investment products are not FDIC-insured, may lose value, and are no bank guarantee. Using margin involves risks: you can lose more than you deposit, you are subject to a margin call, and interest rates may change. The M1 Card is issued by Lincoln Savings Bank, Member FDIC, pursuant to a license from Visa U.S.A., and serviced by M1 Spend LLC. M1 Spend LLC is not a bank. Visa is a registered trademark of Visa Interna\onal Service Associa\on. All product and company names are trademarks™ or registered® trademarks of their respec\ve holders. Use of them does not imply any affilia\on with or endorsement by them. Telephone: 888-714-6674 Address: 213 W Ins\tute Pl, Ste. 301, Chicago, IL 60610 Thank You… Q+A?? Dark Mode Ins & Outs github.com/mccarron/DarkSide Patrick McCarron M1 Finance @McCarron