我的应用程序的很大一部分由web视图组成,以提供尚未通过本机实现提供的功能。网络团队没有计划为网站实施黑暗主题。因此,我的应用程序在iOS 13上的暗模式支持看起来会有点一半一半。

是否可以选择退出暗模式支持,这样我们的应用程序总是显示光模式,以匹配网站主题?


我想我找到解决办法了。我最初从UIUserInterfaceStyle -信息属性列表和UIUserInterfaceStyle - UIKit拼凑在一起,但现在发现它实际上记录在为你的iOS应用程序选择特定的界面风格。

在你的信息里。将UIUserInterfaceStyle (User InterfaceStyle)设置为1 (UIUserInterfaceStyle.light)。

编辑:根据dorbeetle的回答,uiuserinterfacstyle更合适的设置可能是Light。


根据苹果关于“在iOS上实现暗模式”(https://developer.apple.com/videos/play/wwdc2019/214/在31:13开始)的会话,可以在任何视图控制器或视图上设置overrideUserInterfaceStyle为UIUserInterfaceStyleLight或UIUserInterfaceStyleDark,这将被用于任何子视图或视图控制器的traitCollection中。

正如SeanR所提到的,你可以在应用程序的plist文件中设置uiuserinterfacstyle为Light或Dark来改变整个应用程序。


首先,这是苹果关于退出黑暗模式的词条。 这个链接的内容是为Xcode 11和iOS 13编写的:

整个应用程序通过信息。plist文件(Xcode 12)

在你的信息中使用以下键。plist文件:

UIUserInterfaceStyle

并为其赋值Light。

uiuserinterfacstyle赋值的XML:

<key>UIUserInterfaceStyle</key>
<string>Light</string>

UIUserInterfaceStyle的苹果文档


整个应用程序通过信息。plist在构建设置(Xcode 13)


整个应用程序窗口通过窗口属性

你可以针对应用程序的窗口变量设置overrideUserInterfaceStyle。这将应用于出现在窗口中的所有视图。这在iOS 13中是可用的,所以对于支持以前版本的应用程序,你必须包含可用性检查。

根据您的项目是如何创建的,这可能在AppDelegate或SceneDelegate文件中。

if #available(iOS 13.0, *) {
    window?.overrideUserInterfaceStyle = .light
}

单独的UIViewController或UIView

你可以针对UIViewControllers或UIView的overrideUserInterfaceStyle变量设置overrideUserInterfaceStyle。这在iOS 13中是可用的,所以对于支持以前版本的应用程序,你必须包含可用性检查。

斯威夫特

override func viewDidLoad() {
    super.viewDidLoad()
    // overrideUserInterfaceStyle is available with iOS 13
    if #available(iOS 13.0, *) {
        // Always adopt a light interface style.
        overrideUserInterfaceStyle = .light
    }
}

对于Objective-C中的可怜人

if (@available(iOS 13.0, *)) {
        self.overrideUserInterfaceStyle = UIUserInterfaceStyleLight;
}

当针对UIViewController设置时,视图控制器和它的子控制器采用定义的模式。

当针对UIView设置时,视图及其子视图采用已定义的模式。

overrideUserInterfaceStyle的苹果文档


通过SwiftUI View的个人视图

您可以将preferredColorScheme设置为浅色或深色。提供的值将设置演示的配色方案。

import SwiftUI

struct ContentView: View {
    var body: some View {
        Text("Light Only")
            .preferredColorScheme(.light)
    }
}

首选配色方案的苹果文档


感谢@Aron Nelson, @Raimundas Sakalauskas, @NSLeader和@rmaddy用他们的反馈改进了这个答案。


实际上,我写了一些代码,允许你在代码中全局选择退出暗模式,而不需要在应用程序中处理每个视图控制器。通过管理一个类列表,可以细化到在一个类的基础上选择退出。对我来说,我想让我的用户看看他们是否喜欢我的应用程序的黑暗模式界面,如果他们不喜欢,他们可以把它关掉。这将允许他们在其他应用程序中继续使用暗模式。

用户的选择是很好的(看看你苹果,这就是你应该实现的方式)。

它的工作原理是,它只是UIViewController的一个类别。当它加载时,它将原生的viewDidLoad方法替换为一个检查全局标志的方法,以查看是否所有内容都禁用了暗模式。

因为它是在UIViewController加载时触发的,默认情况下它应该自动启动并禁用暗模式。如果这不是你想要的,那么你需要提前在某个地方设置标志,或者只设置默认标志。

我还没有编写任何响应用户打开或关闭标志的代码。这就是示例代码。如果我们想让用户与这个交互,所有的视图控制器都需要重新加载。我不知道如何立即做到这一点,但可能发送一些通知将会做到这一点。现在,这个全局暗模式的开/关只会在应用启动或重启时起作用。

Now, it's not just enough to try to turn off dark mode in every single MFING viewController in your huge app. If you're using color assets you are completely boned. We for 10+ years have understood immutable objects to be immutable. Colors you get from the color asset catalog say they are UIColor but they are dynamic (mutable) colors and will change underneath you as the system changes from dark to light mode. That is supposed to be a feature. But of course there is no master toggle to ask these things to stop making this change (as far as I know right now, maybe someone can improve this).

所以解决方案分为两部分:

a public category on UIViewController that gives some utility and convenience methods... for instance I don't think apple has thought about the fact that some of us mix in web code into our apps. As such we have stylesheets that need to be toggled based on dark or light mode. Thus, you either need to build some kind of a dynamic stylesheet object (which would be good) or just ask what the current state is (bad but easy). this category when it loads will replace the viewDidLoad method of the UIViewController class and intercept calls. I don't know if that breaks app store rules. If it does, there are other ways around that probably but you can consider it a proof of concept. You can for instance make one subclass of all the main view controller types and make all of your own view controllers inherit from those, and then you can use the DarkMode category idea and call into it to force opt out all of your view controllers. It is uglier but it is not going to break any rules. I prefer using the runtime because that's what the runtime was made to do. So in my version you just add the category, you set a global variable on the category for whether or not you want it to block dark mode, and it will do it. You are not out of the woods yet, as mentioned, the other problem is UIColor basically doing whatever the hell it wants. So even if your view controllers are blocking dark mode UIColor doesn't know where or how you're using it so can't adapt. As a result you can fetch it correctly but then it's going to revert on you at some point in the future. Maybe soon maybe later. So the way around that is by allocating it twice using a CGColor and turning it into a static color. This means if your user goes back and re-enables dark mode on your settings page (the idea here is to make this work so that the user has control over your app over and above the rest of the system), all of those static colors need replacing. So far this is left for someone else to solve. The easy ass way to do it is to make a default that you're opting out of dark mode, divide by zero to crash the app since you can't exit it and tell the user to just restart it. That probably violates app store guidelines as well but it's an idea.

The UIColor category doesn't need to be exposed, it just works calling colorNamed: ... if you didn't tell the DarkMode ViewController class to block dark mode, it will work perfectly nicely as expected. Trying to make something elegant instead of the standard apple sphaghetti code which is going to mean you're going to have to modify most of your app if you want to programatically opt out of dark mode or toggle it. Now I don't know if there is a better way of programatically altering the Info.plist to turn off dark mode as needed. As far as my understanding goes that's a compile time feature and after that you're boned.

这是你需要的代码。应该使用一个方法来设置UI样式或在代码中设置默认值。你可以自由地使用,修改,做任何你想做的事情,没有保修,我不知道它是否能通过应用商店。非常欢迎改进。

警告一下,我不使用ARC或任何其他牵手方法。

////// H file

#import <UIKit/UIKit.h>

@interface UIViewController(DarkMode)

// if you want to globally opt out of dark mode you call these before any view controllers load
// at the moment they will only take effect for future loaded view controllers, rather than currently
// loaded view controllers

// we are doing it like this so you don't have to fill your code with @availables() when you include this
typedef enum {
    QOverrideUserInterfaceStyleUnspecified,
    QOverrideUserInterfaceStyleLight,
    QOverrideUserInterfaceStyleDark,
} QOverrideUserInterfaceStyle;

// the opposite condition is light interface mode
+ (void)setOverrideUserInterfaceMode:(QOverrideUserInterfaceStyle)override;
+ (QOverrideUserInterfaceStyle)overrideUserInterfaceMode;

// utility methods
// this will tell you if any particular view controller is operating in dark mode
- (BOOL)isUsingDarkInterfaceStyle;
// this will tell you if any particular view controller is operating in light mode mode
- (BOOL)isUsingLightInterfaceStyle;

// this is called automatically during all view controller loads to enforce a single style
- (void)tryToOverrideUserInterfaceStyle;

@end


////// M file


//
//  QDarkMode.m

#import "UIViewController+DarkMode.h"
#import "q-runtime.h"


@implementation UIViewController(DarkMode)

typedef void (*void_method_imp_t) (id self, SEL cmd);
static void_method_imp_t _nativeViewDidLoad = NULL;
// we can't @available here because we're not in a method context
static long _override = -1;

+ (void)load;
{
#define DEFAULT_UI_STYLE UIUserInterfaceStyleLight
    // we won't mess around with anything that is not iOS 13 dark mode capable
    if (@available(iOS 13,*)) {
        // default setting is to override into light style
        _override = DEFAULT_UI_STYLE;
        /*
         This doesn't work...
        NSUserDefaults *d = NSUserDefaults.standardUserDefaults;
        [d setObject:@"Light" forKey:@"UIUserInterfaceStyle"];
        id uiStyle = [d objectForKey:@"UIUserInterfaceStyle"];
        NSLog(@"%@",uiStyle);
         */
        if (!_nativeViewDidLoad) {
            Class targetClass = UIViewController.class;
            SEL targetSelector = @selector(viewDidLoad);
            SEL replacementSelector = @selector(_overrideModeViewDidLoad);
            _nativeViewDidLoad = (void_method_imp_t)QMethodImplementationForSEL(targetClass,targetSelector);
            QInstanceMethodOverrideFromClass(targetClass, targetSelector, targetClass, replacementSelector);
        }
    }
}

// we do it like this because it's not going to be set often, and it will be tested often
// so we can cache the value that we want to hand to the OS
+ (void)setOverrideUserInterfaceMode:(QOverrideUserInterfaceStyle)style;
{
    if (@available(iOS 13,*)){
        switch(style) {
            case QOverrideUserInterfaceStyleLight: {
                _override = UIUserInterfaceStyleLight;
            } break;
            case QOverrideUserInterfaceStyleDark: {
                _override = UIUserInterfaceStyleDark;
            } break;
            default:
                /* FALLTHROUGH - more modes can go here*/
            case QOverrideUserInterfaceStyleUnspecified: {
                _override = UIUserInterfaceStyleUnspecified;
            } break;
        }
    }
}
+ (QOverrideUserInterfaceStyle)overrideUserInterfaceMode;
{
    if (@available(iOS 13,*)){
        switch(_override) {
            case UIUserInterfaceStyleLight: {
                return QOverrideUserInterfaceStyleLight;
            } break;
            case UIUserInterfaceStyleDark: {
                return QOverrideUserInterfaceStyleDark;
            } break;
            default:
                /* FALLTHROUGH */
            case UIUserInterfaceStyleUnspecified: {
                return QOverrideUserInterfaceStyleUnspecified;
            } break;
        }
    } else {
        // we can't override anything below iOS 12
        return QOverrideUserInterfaceStyleUnspecified;
    }
}

- (BOOL)isUsingDarkInterfaceStyle;
{
    if (@available(iOS 13,*)) {
        if (self.traitCollection.userInterfaceStyle == UIUserInterfaceStyleDark){
            return YES;
        }
    }
    return NO;
}

- (BOOL)isUsingLightInterfaceStyle;
{
    if (@available(iOS 13,*)) {
        if (self.traitCollection.userInterfaceStyle == UIUserInterfaceStyleLight){
            return YES;
        }
        // if it's unspecified we should probably assume light mode, esp. iOS 12
    }
    return YES;
}

- (void)tryToOverrideUserInterfaceStyle;
{
    // we have to check again or the compile will bitch
    if (@available(iOS 13,*)) {
        [self setOverrideUserInterfaceStyle:(UIUserInterfaceStyle)_override];
    }
}

// this method will be called via the viewDidLoad chain as we will patch it into the
// UIViewController class
- (void)_overrideModeViewDidLoad;
{
    if (_nativeViewDidLoad) {
        _nativeViewDidLoad(self,@selector(viewDidLoad));
    }
    [self tryToOverrideUserInterfaceStyle];
}


@end

// keep this in the same file, hidden away as it needs to switch on the global ... yeah global variables, I know, but viewDidLoad and colorNamed: are going to get called a ton and already it's adding some inefficiency to an already inefficient system ... you can change if you want to make it a class variable. 

// this is necessary because UIColor will also check the current trait collection when using asset catalogs
// so we need to repair colorNamed: and possibly other methods
@interface UIColor(DarkMode)
@end

@implementation UIColor (DarkMode)

typedef UIColor *(*color_method_imp_t) (id self, SEL cmd, NSString *name);
static color_method_imp_t _nativeColorNamed = NULL;
+ (void)load;
{
    // we won't mess around with anything that is not iOS 13 dark mode capable
    if (@available(iOS 13,*)) {
        // default setting is to override into light style
        if (!_nativeColorNamed) {
            // we need to call it once to force the color assets to load
            Class targetClass = UIColor.class;
            SEL targetSelector = @selector(colorNamed:);
            SEL replacementSelector = @selector(_overrideColorNamed:);
            _nativeColorNamed = (color_method_imp_t)QClassMethodImplementationForSEL(targetClass,targetSelector);
            QClassMethodOverrideFromClass(targetClass, targetSelector, targetClass, replacementSelector);
        }
    }
}


// basically the colors you get
// out of colorNamed: are dynamic colors... as the system traits change underneath you, the UIColor object you
// have will also change since we can't force override the system traits all we can do is force the UIColor
// that's requested to be allocated out of the trait collection, and then stripped of the dynamic info
// unfortunately that means that all colors throughout the app will be static and that is either a bug or
// a good thing since they won't respond to the system going in and out of dark mode
+ (UIColor *)_overrideColorNamed:(NSString *)string;
{
    UIColor *value = nil;
    if (@available(iOS 13,*)) {
        value = _nativeColorNamed(self,@selector(colorNamed:),string);
        if (_override != UIUserInterfaceStyleUnspecified) {
            // the value we have is a dynamic color... we need to resolve against a chosen trait collection
            UITraitCollection *tc = [UITraitCollection traitCollectionWithUserInterfaceStyle:_override];
            value = [value resolvedColorWithTraitCollection:tc];
        }
    } else {
        // this is unreachable code since the method won't get patched in below iOS 13, so this
        // is left blank on purpose
    }
    return value;
}
@end

这里有一组实用函数用于进行方法交换。单独的文件中。这是标准的东西,你可以在任何地方找到类似的代码。

// q-runtime.h

#import <Foundation/Foundation.h>
#import <objc/message.h>
#import <stdatomic.h>

// returns the method implementation for the selector
extern IMP
QMethodImplementationForSEL(Class aClass, SEL aSelector);

// as above but gets class method
extern IMP
QClassMethodImplementationForSEL(Class aClass, SEL aSelector);


extern BOOL
QClassMethodOverrideFromClass(Class targetClass, SEL targetSelector,
                              Class replacementClass, SEL replacementSelector);

extern BOOL
QInstanceMethodOverrideFromClass(Class targetClass, SEL targetSelector,
                                 Class replacementClass, SEL replacementSelector);


// q-runtime.m

static BOOL
_QMethodOverride(Class targetClass, SEL targetSelector, Method original, Method replacement)
{
    BOOL flag = NO;
    IMP imp = method_getImplementation(replacement);
    // we need something to work with
    if (replacement) {
        // if something was sitting on the SEL already
        if (original) {
            flag = method_setImplementation(original, imp) ? YES : NO;
            // if we're swapping, use this
            //method_exchangeImplementations(om, rm);
        } else {
            // not sure this works with class methods...
            // if it's not there we want to add it
            flag = YES;
            const char *types = method_getTypeEncoding(replacement);
            class_addMethod(targetClass,targetSelector,imp,types);
            XLog_FB(red,black,@"Not sure this works...");
        }
    }
    return flag;
}

BOOL
QInstanceMethodOverrideFromClass(Class targetClass, SEL targetSelector,
                                 Class replacementClass, SEL replacementSelector)
{
    BOOL flag = NO;
    if (targetClass && replacementClass) {
        Method om = class_getInstanceMethod(targetClass,targetSelector);
        Method rm = class_getInstanceMethod(replacementClass,replacementSelector);
        flag = _QMethodOverride(targetClass,targetSelector,om,rm);
    }
    return flag;
}


BOOL
QClassMethodOverrideFromClass(Class targetClass, SEL targetSelector,
                              Class replacementClass, SEL replacementSelector)
{
    BOOL flag = NO;
    if (targetClass && replacementClass) {
        Method om = class_getClassMethod(targetClass,targetSelector);
        Method rm = class_getClassMethod(replacementClass,replacementSelector);
        flag = _QMethodOverride(targetClass,targetSelector,om,rm);
    }
    return flag;
}

IMP
QMethodImplementationForSEL(Class aClass, SEL aSelector)
{
    Method method = class_getInstanceMethod(aClass,aSelector);
    if (method) {
        return method_getImplementation(method);
    } else {
        return NULL;
    }
}

IMP
QClassMethodImplementationForSEL(Class aClass, SEL aSelector)
{
    Method method = class_getClassMethod(aClass,aSelector);
    if (method) {
        return method_getImplementation(method);
    } else {
        return NULL;
    }
}

因为q-runtime.h是我的可重用库,这只是它的一部分,所以我将它从几个文件中复制粘贴出来。如果有些东西没有编译让我知道。


如果你想退出整个应用程序,上面的答案是有效的。如果你在有UI的库上工作,并且你没有编辑.plist的奢侈,你也可以通过代码来做。

如果你正在编译iOS 13 SDK,你可以简单地使用以下代码:

迅速:

if #available(iOS 13.0, *) {
    self.overrideUserInterfaceStyle = .light
}

Obj - c:

if (@available(iOS 13.0, *)) {
    self.overrideUserInterfaceStyle = UIUserInterfaceStyleLight;
}

然而,如果你希望你的代码也能在iOS 12 SDK(目前仍然是最新的稳定SDK)上编译,你应该使用选择器。使用选择器的代码:

Swift (XCode将显示此代码的警告,但这是目前唯一的方法,因为属性不存在于SDK 12,因此不会编译):

if #available(iOS 13.0, *) {
    if self.responds(to: Selector("overrideUserInterfaceStyle")) {
        self.setValue(UIUserInterfaceStyle.light.rawValue, forKey: "overrideUserInterfaceStyle")
    }
}

Obj - c:

if (@available(iOS 13.0, *)) {
    if ([self respondsToSelector:NSSelectorFromString(@"overrideUserInterfaceStyle")]) {
        [self setValue:@(UIUserInterfaceStyleLight) forKey:@"overrideUserInterfaceStyle"];
    }
}

如果你将添加UIUserInterfaceStyle键到plist文件中,苹果可能会拒绝发布构建,如这里所述:https://stackoverflow.com/a/56546554/7524146 无论如何,显式地告诉每个ViewController self是很烦人的。overrideuserinterfacstyle = .light。但是你可以在根窗口对象上使用这段代码:

if #available(iOS 13.0, *) {
    if window.responds(to: Selector(("overrideUserInterfaceStyle"))) {
        window.setValue(UIUserInterfaceStyle.light.rawValue, forKey: "overrideUserInterfaceStyle")
    }
}

请注意,你不能在应用程序(application: didFinishLaunchingWithOptions:)中这样做,因为这个选择器在早期阶段不会响应true。但你可以以后再做。如果你在你的应用中使用自定义AppPresenter或AppRouter类,而不是在AppDelegate中自动启动UI,这非常简单。


除了其他回答,根据我对以下问题的理解,你只需要在对iOS 13 SDK(使用XCode 11)编译时准备Dark模式。

系统假设应用程序链接到iOS 13或更高版本的SDK 支持光明和黑暗的外观。在iOS中,你指定 通过指定特定的界面样式来指定您想要的特定外观 到您的窗口、视图或视图控制器。您也可以禁用支持 黑暗模式完全使用信息。plist关键。

Link


最新更新,

如果你用的是Xcode 10。x,那么默认的UIUserInterfaceStyle是light对于iOS 13.x。当在iOS 13设备上运行时,它只能在灯光模式下工作。

不需要显式地在信息中添加UIUserInterfaceStyle键。plist文件,添加它将给出一个错误时,你验证你的应用程序,说:

无效的信息。plist关键。载荷/AppName.appInfo中的键'UIUserInterfaceStyle'。Plist文件无效。

只在Info中添加uiuserinterfacstyle键。plist文件时使用Xcode 11.x。


对于整个应用程序:(在信息。plist文件):

<key>UIUserInterfaceStyle</key>
<string>Light</string>


窗口(通常是整个应用程序):

window!.overrideUserInterfaceStyle = .light

你可以从SceneDelegate中获取窗口


UIViewController:

viewController.overrideUserInterfaceStyle = .light

你可以设置任何viewController,甚至在viewController内部


UIView:

view.overrideUserInterfaceStyle = .light

你可以设置任何视图,甚至在视图内部

如果你支持早期的iOS版本,你可能需要使用if #available(iOS 13.0, *){…}。


SwiftUI View):

.preferredColorScheme(.light) <- This Modifier

or

.environment(\.colorScheme, .light) <- This Modifier

如果你没有使用Xcode 11或更高版本(即iOS 13或更高版本的SDK),你的应用程序没有自动选择支持暗模式。所以,没有必要选择退出黑暗模式。

如果你使用的是Xcode 11或更高版本,系统会自动为你的应用启用暗模式。根据你的喜好,有两种方法来禁用暗模式。你可以完全禁用它,也可以为任何特定的窗口、视图或视图控制器禁用它。

禁用黑暗模式完全为您的应用程序

你可以通过在你的应用的信息中包含一个值为Light的UIUserInterfaceStyle键来禁用暗模式。plist文件。 这忽略了用户的偏好,总是应用浅色外观的应用程序。

禁用窗口、视图或视图控制器的暗模式

通过设置适当窗口、视图或视图控制器的overrideUserInterfaceStyle属性,可以强制界面始终以浅色或深色样式显示。

视图控制器:

override func viewDidLoad() {
    super.viewDidLoad()
    /* view controller’s views and child view controllers 
     always adopt a light interface style. */
    overrideUserInterfaceStyle = .light
}

视图:

// The view and all of its subviews always adopt light style.
youView.overrideUserInterfaceStyle = .light

亮点:

/* Everything in the window adopts the style, 
 including the root view controller and all presentation controllers that 
 display content in that window.*/
window.overrideUserInterfaceStyle = .light

注意:苹果强烈鼓励在你的应用中支持暗模式。 你只能暂时禁用黑暗模式。

阅读更多:为你的iOS应用程序选择特定的界面风格


是的,你可以跳过添加以下代码在viewDidLoad:

if #available(iOS 13.0, *) {
        // Always adopt a light interface style.
        overrideUserInterfaceStyle = .light
    }

我的应用程序不支持黑暗模式,现在使用一个浅的应用程序栏颜色。我可以通过在Info.plist中添加以下键来强制状态栏内容为深色文本和图标:

<key>UIStatusBarStyle</key>
<string>UIStatusBarStyleDarkContent</string>
<key>UIUserInterfaceStyle</key>
<string>Light</string>
<key>UIViewControllerBasedStatusBarAppearance</key>
<true/>

在这里找到其他可能的值:https://developer.apple.com/documentation/uikit/uistatusbarstyle

颤振的用户

不要忘记在Flutter应用程序栏上设置应用程序栏亮度属性,如下所示:

AppBar(
    backgroundColor: Colors.grey[100],
    brightness: Brightness.light, // <---------
    title: const Text('Hi there'),
),

********** Xcode 11以上的最容易的方法 ***********

将此添加到信息中。Plist before </dict></ Plist >

<key>UIUserInterfaceStyle</key>
<string>Light</string>

我将使用这个解决方案,因为窗口属性可能会在应用程序生命周期中发生变化。因此需要重复分配" overrideuserinterfacstyle = .light"。UIWindow.appearance()使我们能够为新创建的UIWindow对象设置默认值。

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

      if #available(iOS 13.0, *) {
          UIWindow.appearance().overrideUserInterfaceStyle = .light
      }

      return true
    }
}

这里有一些小贴士和技巧,你可以在你的应用程序中使用来支持或绕过黑暗模式。

第一个技巧:重写ViewController样式

你可以覆盖UIViewController的界面风格

1: overrideuserinterfacstyle = .dark //暗模式 2: overrideuserinterfacstyle = .light //光照模式

class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        overrideUserInterfaceStyle = .light    
    }
}

第二个技巧:在info.plist中添加一个键

您可以简单地添加一个新密钥

UIUserInterfaceStyle

在你的应用程序信息。plist,并将其值设置为Light或Dark。这将覆盖应用程序默认样式为您提供的值。 你不需要在每个viewController中添加overrideUserInterfaceStyle = .light这一行,只需在info中添加一行即可。就是这样。


只需简单地添加以下关键在您的信息。Plist文件:

<key>UIUserInterfaceStyle</key>
    <string>Light</string>

在Xcode 11中,你可以在整个应用中关闭暗模式:

去Info.plist 添加波纹管 <键> UIUserInterfaceStyle关键> < / 光<字符串> < /字符串>

信息。Plist将如下所示…


只需在info中添加这些行。plist文件:

<key>UIUserInterfaceStyle</key>
<string>light</string>

这将迫使应用程序只在轻模式下运行。


 if #available(iOS 13.0, *) {
            overrideUserInterfaceStyle = .light
        } else {
            // Fallback on earlier versions
        }

objective - c版本

 if (@available(iOS 13.0, *)) {
        _window.overrideUserInterfaceStyle = UIUserInterfaceStyleLight;
    }

import UIKit

extension UIViewController {

    override open func awakeFromNib() {

        super.awakeFromNib()

        if #available(iOS 13.0, *) {

            overrideUserInterfaceStyle = .light

        }

    }
}

你可以这样做:添加这个新键uiuserinterfacstyle到Info。plist,并将其值设置为Light。并检查警报控制器出现轻模式。

UIUserInterfaceStyle 光 如果你在你的整个应用程序中强制亮/暗模式,而不管用户的设置,通过添加关键uiuserinterfacstyle到你的信息。plist文件并将其值设置为Light或Dark。


这个问题有很多答案,而不是在信息中使用它。plist你可以在AppDelegate中这样设置:

#if compiler(>=5.1)
        if #available(iOS 13.0, *) {
            self.window?.overrideUserInterfaceStyle = .light
        }
        #endif

在Xcode 11.3和iOS 13.3上测试


斯威夫特5

您可以通过以下链接下载演示项目:- https://github.com/rashidlatif55/DarkLightMode

两种方法切换暗到亮模式:

1- info.plist

    <key>UIUserInterfaceStyle</key>
    <string>Light</string>

2-程序化或运行时

  @IBAction private func switchToDark(_ sender: UIButton){
        UIApplication.shared.windows.first?.overrideUserInterfaceStyle = .dark
    }

Xcode 12和iOS 14更新。我已经尝试了之前的选项选择退出黑暗模式和这句话在信息。Plist文件不适合我:

<key>UIUserInterfaceStyle</key>
<string>Light</string>

现在它被重命名为:

<key>Appearance</key>
<string>Light</string>

此设置将阻止整个应用程序中的所有黑暗模式。

编辑:

修复了错别字,谢谢@sarah


在Xcode 12中,你可以将add更改为“appearance”。这会有用的!!


将此添加到info.plist

<key>UIUserInterfaceStyle</key>
    <string>Light</string>

iOS 14.3和Xcode 12.3更新

在信息。plist文件添加外观为光。

<key>Appearance</key>
<string>Light</string>

是的. .你可以在iOS项目中添加以下设置。

在信息。将uiuserinterfacstyle添加到Light中。

如果你的项目在IONIC..您可以在配置文件中添加以下设置

<platform name="ios">
  <edit-config file="*-Info.plist" mode="merge" target="UIUserInterfaceStyle">
  <string>Light</string>
 </edit-config>
</platform>

使用这些设置,设备暗模式不会影响你的应用程序。


在ViewController.swift文件中添加overrideUserInterfaceStyle = .light或在info中更改Appearance为“light”。plist文件