美文网首页
iOS swift暗黑模式适配

iOS swift暗黑模式适配

作者: fordG | 来源:发表于2021-01-13 16:07 被阅读0次
    AYlQRxqdr6.gif
    • 暗黑模式适配 跟随系统 自定义切换
    import UIKit
    
    /*
        默认跟随系统暗黑模式
        关闭系统暗黑模式, 用自定义的暗黑模式
     */
    
    let DarkToSystem = "GJDarkToSystem"
    let LightDark = "GJLightDark"
    
    // true 跟随系统 false 自选模式
    var floorSystem: Bool {
        get {
            //默认跟随系统
            let value = UserDefaults.standard.value(forKey: DarkToSystem)
            if var _: Bool = value as? Bool {
                return UserDefaults.standard.bool(forKey: DarkToSystem)
            }
            //第一次不存在
            return true
        }
        set(newValue){
            if(newValue){
                DarkUtil.share.setSystemDark()
            }
            UserDefaults.standard.set(newValue, forKey: DarkToSystem)
            UserDefaults.standard.synchronize()
        }
    }
    
    // true light false dark
    var isLight: Bool {
        get {
            //默认light
            let value = UserDefaults.standard.value(forKey: LightDark)
            if var _: Bool = value as? Bool {
                return UserDefaults.standard.bool(forKey: LightDark)
            }
            //第一次不存在
            return true
        }
        set(newValue){
            DarkUtil.share.setDark(newValue)
            UserDefaults.standard.set(newValue, forKey: LightDark)
            UserDefaults.standard.synchronize()
        }
    }
    
    class DarkUtil {
        
        //单例模式
        static let share = DarkUtil()
        
        //获取SceneDelegate
        lazy var screnDelegate: SceneDelegate? = {
            var uiScreen: UIScene?
            UIApplication.shared.connectedScenes.forEach { (screen) in
                uiScreen = screen
            }
            return (uiScreen?.delegate as? SceneDelegate)
        }()
    
        init() {
            
        }
        
        func setSystemDark(){
            screnDelegate?.window?.overrideUserInterfaceStyle = UITraitCollection.current.userInterfaceStyle
        }
        
        func defaultDark() {
            //默认跟随系统暗黑模式开启监听
            if(!floorSystem){
                setDark(isLight)
            }else{
                setSystemDark()
            }
        }
        
        func setDark(_ b: Bool){
            if(b){
                //light
                screnDelegate?.window?.overrideUserInterfaceStyle = .light
            }else{
                //dark
                screnDelegate?.window?.overrideUserInterfaceStyle = .dark
            }
        }
        
        static func colorLightDark(light: UIColor, dark: UIColor) -> UIColor {
            if #available(iOS 13.0, *) {
                return UIColor.init { (trainCollection) -> UIColor in
                    if trainCollection.userInterfaceStyle == .light {
                        return light
                    }else{
                        if(floorSystem){
                            return dark
                        }else{
                            if(isLight){
                                return light
                            }else{
                                return dark
                            }
                        }
                    }
                }
            }
            return light
        }
    }
    
    • 具体ui可以自己话,下面是简单的点击swtich事件
    @IBAction func systemSwtichTap(_ sender: UISwitch) {
            print(sender.isOn)
            if(sender.isOn){
                darkContentView.isHidden = true
                floorSystem = true
                //跟随系统设置
                isLight = true
                darkSwitch.setOn(false, animated: true)
            }else{
                darkContentView.isHidden = false
                floorSystem = false
            }
        }
        
        @IBAction func darkSwtichTap(_ sender: UISwitch) {
            print(sender.isOn)
            if(sender.isOn){
                //自定义的暗黑模式
                isLight = false
            }else{
                isLight = true
            }
        }
    
    • 颜色设置代码
    DarkUtil.colorLightDark(light: .white, dark: .purple)
    
    • SceneDelegate 加上这个默认的模式
    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
            // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
            // If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
            // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
            
            //跟随系统暗黑模式开启监听
            DarkUtil.share.defaultDark()
            guard let _ = (scene as? UIWindowScene) else { return }
        }
    

    相关文章

      网友评论

          本文标题:iOS swift暗黑模式适配

          本文链接:https://www.haomeiwen.com/subject/ewauaktx.html