How can I check whether dark mode is enabled in iOS/iPadOS?

前端 未结 16 1433
囚心锁ツ
囚心锁ツ 2020-12-08 09:41

Starting from iOS/iPadOS 13, a dark user interface style is available, similar to the dark mode introduced in macOS Mojave. How can I check whether the user has enabled the

相关标签:
16条回答
  • 2020-12-08 10:07

    The best point to detect changes is traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) function of UIView/UIViewController.

    override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
        super.traitCollectionDidChange(previousTraitCollection)
    
        let userInterfaceStyle = traitCollection.userInterfaceStyle // Either .unspecified, .light, or .dark
        // Update your user interface based on the appearance
    }
    

    Detecting appearance changes is trivial by overriding traitCollectionDidChange on view controllers. Then, just access the view controller’s traitCollection.userInterfaceStyle.

    However, it is important to remember that traitCollectionDidChange may be called for other trait changes, such as the device rotating. You can check if the current appearance is different using this new method:

    override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
        super.traitCollectionDidChange(previousTraitCollection)
    
        let hasUserInterfaceStyleChanged = previousTraitCollection.hasDifferentColorAppearance(comparedTo: traitCollection) // Bool
        // Update your user interface based on the appearance
    }
    
    0 讨论(0)
  • 2020-12-08 10:08

    You can use the following code to check for light, or dark mode in your project:

    func viewDidLoad() {
        super.viewDidLoad()
    
        switch traitCollection.userInterfaceStyle {
            case .light, .unspecified:
                // light mode detected
            case .dark:
                // dark mode detected
        }
    }
    

    You can also check for changes in the interface style:

    override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
        super.traitCollectionDidChange(previousTraitCollection)
    
        let userInterfaceStyle = traitCollection.userInterfaceStyle // Either .unspecified, .light, or .dark
        // Update your user interface based on the appearance
    }
    

    Just like in macOS since Mojave, you can define images for both light and dark mode in your asset catalog so that those images will be used automatically:

    0 讨论(0)
  • 2020-12-08 10:09

    1/ for UIView/UIViewController:

    self.traitCollection.userInterfaceStyle == .dark
    

    2/ for static or other:

    UITraitCollection.current.userInterfaceStyle == .dark
    

    BUT:

    //Never use this! You will get wrong value in app extensions (ex. ToDay widget)
    UIScreen.main.traitCollection.userInterfaceStyle == .dark //WRONG!
    
    0 讨论(0)
  • 2020-12-08 10:09

    Objective C

    To detect when dark mode is enabled or disabled via the Control Centre use an "appDidBecomeActive" notification that will be triggered when you return to your app.

    //----------------------------------------------------------------------------
    //                          viewWillAppear
    //----------------------------------------------------------------------------
    - (void)viewWillAppear {
        [super viewWillAppear];
    
        [[NSNotificationCenter defaultCenter]addObserver:self
                                       selector:@selector(appDidBecomeActive:)
                                       name:UIApplicationDidBecomeActiveNotification
                                       object:nil];
    
    }
    

    Don't forget to remove it when you're finished:

    //------------------------------------------------------------------------------------
    //                    viewWillDisappear
    //------------------------------------------------------------------------------------
    - (void)viewWillDisappear:(BOOL)animated
    {
        [super viewWillDisappear:animated];
    
        [[NSNotificationCenter defaultCenter] removeObserver:self        
                                     name:UIApplicationDidBecomeActiveNotification 
                                     object:nil];
    
    }
    

    Do what ever you need to when dark mode changes:

    //----------------------------------------------------------------------------
    //                          appDidBecomeActive
    //----------------------------------------------------------------------------
    -(void)appDidBecomeActive:(NSNotification*)note {
        if (@available(iOS 13.0, *)) {
            if( self.traitCollection.userInterfaceStyle == UIUserInterfaceStyleDark ){
                //dark mode
            }
            else {
                //not dark mode
            }
        }
        else {
            //fall back for older versions
        }
    }
    
    0 讨论(0)
提交回复
热议问题