【问题标题】:How we Support Dark Mode in Current iOS App with iOS 13?我们如何在 iOS 13 的当前 iOS 应用中支持暗模式?
【发布时间】:2019-11-02 21:13:45
【问题描述】:

我当前的应用程序是用 objC 和 Swift 开发的。我需要支持暗模式。谁能建议我如何在全球范围内实现这一目标?

【问题讨论】:

  • 看看 UIAppearance() 有一个 2 结构的颜色,浅色和深色
  • 在 appdelegate 中对吗?还是其他地方?
  • 当您检测到暗/亮模式时,这取决于代码中的点。例如,您将如何在这两个主题之间进行切换?
  • 你能提供一些示例代码吗?我只想用按钮切换!
  • 是的,我正要告诉你更新标题,但你做到了。不幸的是,我对此一无所知

标签: ios iphone ios13 ios-darkmode


【解决方案1】:

Xcode11-beta 的界面构建器中提供了几种语义系统颜色。请使用它们来支持 .light 和 .darkMode

请按照基本说明一步一步来。

  1. UIView - 使用界面生成器中的 systemBackgroundColor。
  2. UILabel - 使用界面生成器中的 defaulLabel 颜色。
  3. CustomView - 请使用 .xcassests 目录并创建 新颜色集,为 .light 和 .darkMode 添加 属性检查器 中的 appearances 选项以及为两种模式提供不同的颜色。
  4. CustomImages - 请使用 .xcassests 目录并从 attributes inspector 中为 .light 和 .darkMode 添加 appearances 选项,并为这两种模式提供不同的图像。
  5. 还有一个选项可以使用代码提供不同的颜色和不同的图像。

    override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
        super.traitCollectionDidChange(previousTraitCollection)
        updateUIForiOS13()
    }
    
    private func isDarkMode() -> Bool{
        if #available(iOS 12.0, *) {
            let isDark = traitCollection.userInterfaceStyle == .dark ? true : false
            return isDark
        }
        return false
    }
    

【讨论】:

    【解决方案2】:

    利用 Swift 模块将包含 UIColor 扩展代码的 .swift 文件包含到您的 Objective-C 代码中。它可以在 Your_Target > Build Settings > Objective-C Generated Interface Header Name

    下找到

    这将生成一个头文件“MyApp-Swift.h”

    然后将@objc 添加到包含 UIColor 扩展代码的 .swift 文件中的每个静态颜色函数中,以便为 Objective-C 公开它。

    @objc static func color_three() -> UIColor {
    
        return themeConvertor(dark: "#000000", light: "#FFFFFF")
    
    }
    

    在您的 Objective-C .m 文件中,导入模块,然后从 UIColor 扩展中引用颜色函数:

    #import "MyApp-Swift.h" // swift module
    
    - (void)awakeFromNib {
    
        [super awakeFromNib];
    
        // color
    
        textLabel.textColor = [UIColor color_three];
    
    }
    

    游乐场示例

    //: A UIKit based Playground for presenting user interface
    
    import UIKit
    import PlaygroundSupport
    
    class MyViewController : UIViewController {
    
        override func loadView() {
    
            // color_one
    
            let view = UIView()
            view.backgroundColor = UIColor.color_one()
    
            // color_two
    
            let label = UILabel()
            label.frame = CGRect(x: 150, y: 200, width: 200, height: 20)
            label.text = "Hello World!"
            label.textColor = UIColor.color_two()
    
            view.addSubview(label)
            self.view = view
    
        }
    
    }
    
    // put this into a file called UIColor+Extensions.swift
    
    extension UIColor {
    
        static func themeConvertor(dark: String, light: String) -> UIColor {
    
            if #available(iOS 13, *) {
    
                return UIColor.init { (trait) -> UIColor in
    
                    // the color can be from your own color config struct as well.
    
                    return trait.userInterfaceStyle == .dark ? UIColor.init(hex: dark)! : UIColor.init(hex: light)!
    
                }
    
            } else {
    
                return UIColor.init(hex: light)!
    
            }
    
        }
    
        // Color 1
        // Black & White
    
        @objc static func color_one() -> UIColor {
    
            return themeConvertor(dark: "#000000", light: "#FFFFFF")
    
        }
    
        // Color 2
        // Orange & Blue
    
        @objc static func color_two() -> UIColor {
    
            return themeConvertor(dark: "#FFA500", light: "#0000FF")
    
        }
    
        // Color from HEX
    
        convenience init(r: UInt8, g: UInt8, b: UInt8, alpha: CGFloat = 1.0) {
    
            let divider: CGFloat = 255.0
    
            self.init(red: CGFloat(r)/divider, green: CGFloat(g)/divider, blue: CGFloat(b)/divider, alpha: alpha)
    
        }
    
        private convenience init(rgbWithoutValidation value: Int32, alpha: CGFloat = 1.0) {
    
            self.init(
                r: UInt8((value & 0xFF0000) >> 16),
                g: UInt8((value & 0x00FF00) >> 8),
                b: UInt8(value & 0x0000FF),
                alpha: alpha
            )
    
        }
    
        convenience init?(rgb: Int32, alpha: CGFloat = 1.0) {
    
            if rgb > 0xFFFFFF || rgb < 0 {
    
                return nil
    
            }
    
            self.init(rgbWithoutValidation: rgb, alpha: alpha)
    
        }
    
        convenience init?(hex: String, alpha: CGFloat = 1.0) {
    
            var charSet = CharacterSet.whitespacesAndNewlines
            charSet.insert("#")
    
            let _hex = hex.trimmingCharacters(in: charSet)
    
            guard _hex.range(of: "^[0-9A-Fa-f]{6}$", options: .regularExpression) != nil else {
    
                return nil
    
            }
    
            var rgb: UInt32 = 0
            Scanner(string: _hex).scanHexInt32(&rgb)
    
            self.init(rgbWithoutValidation: Int32(rgb), alpha: alpha)
    
        }
    
    }
    
    // Present the view controller in the Live View window
    PlaygroundPage.current.liveView = MyViewController()
    

    【讨论】:

      【解决方案3】:

      这里是添加颜色逻辑的代码,应该出现在深色模式中。

      if self.traitCollection.userInterfaceStyle == .dark {
      
        //Add your Dark mode colors here
       } else {
      
        //Your normal colors should appear here
       }
      

      要了解有关在 iOS 应用程序中调整暗模式的更多信息,请参阅以下博客文章。

      How to Adopt iOS 13 Dark Mode in your iOS App

      【讨论】:

        【解决方案4】:

        你可以试试这个方法:

        对于颜色:

        let view = UIView()
        view.backdroundColor = UIColor(named: "Color")
        

        对于图像:

        let imageView = UIImageView()
        imageView.image = UIImage(named: "Image")
        

        medium post 获得此信息

        【讨论】:

          猜你喜欢
          • 2020-07-27
          • 2020-01-19
          • 2019-12-03
          • 1970-01-01
          • 2020-02-04
          • 2020-01-18
          • 2020-02-26
          • 2019-11-15
          • 2021-12-02
          相关资源
          最近更新 更多