Sea*_*anR 143 ios13 ios-darkmode
我的应用程序很大一部分由Web视图组成,以提供本机实现尚不可用的功能。Web团队没有计划为网站实施深色主题。因此,在iOS 13上支持深色模式的情况下,我的应用看起来会一半/一半。
是否可以选择不支持暗模式,以便我们的应用程序始终显示亮模式以匹配网站主题?
Cod*_*der 392
首先,这是与退出暗模式有关的Apple条目。 此链接的内容适用于Xcode 11和iOS 13:
如果您希望退出整个申请
在info.plist文件中使用以下密钥:
UIUserInterfaceStyle
Run Code Online (Sandbox Code Playgroud)
并为其分配值Light。
该XML的UIUserInterfaceStyle分配:
<key>UIUserInterfaceStyle</key>
<string>Light</string>
Run Code Online (Sandbox Code Playgroud)
您可以overrideUserInterfaceStyle针对应用程序的window变量进行设置。
根据项目的创建方式,该AppDelegate文件可能位于文件中,也可能位于中SceneDelegate。
if #available(iOS 13.0, *) {
window?.overrideUserInterfaceStyle = .light
}
Run Code Online (Sandbox Code Playgroud)
如果您希望逐个退出UIViewController
override func viewDidLoad() {
super.viewDidLoad()
// overrideUserInterfaceStyle is available with iOS 13
if #available(iOS 13.0, *) {
// Always adopt a light interface style.
overrideUserInterfaceStyle = .light
}
}
Run Code Online (Sandbox Code Playgroud)
Apple文档overrideUserInterfaceStyle
上面的代码在Xcode 11中的样子:
如果您使用Xcode 11进行提交,则可以放心忽略此行下的所有内容。
由于相关的API在iOS 12中不存在,因此在尝试使用上面提供的值时会出现错误:
对于设置overrideUserInterfaceStyle在UIViewController
如果您希望逐个退出UIViewController
这可以通过测试编译器版本和iOS版本在Xcode 10中进行处理:
#if compiler(>=5.1)
if #available(iOS 13.0, *) {
// Always adopt a light interface style.
overrideUserInterfaceStyle = .light
}
#endif
Run Code Online (Sandbox Code Playgroud)
如果您希望退出整个申请
通过将以下代码添加到AppDelegate文件中,您可以修改上述代码段以使其适用于Xcode 10的整个应用程序。
#if compiler(>=5.1)
if #available(iOS 13.0, *) {
// Always adopt a light interface style.
window?.overrideUserInterfaceStyle = .light
}
#endif
Run Code Online (Sandbox Code Playgroud)
但是,使用Xcode版本10.x时,plist设置将失败:
感谢@Aron尼尔森,@Raimundas Sakalauskas, @NSLeader和rmaddy为改善这样的回答与他们的反馈。
dor*_*tle 146
据苹果公司的“实施暗模式iOS上的”会话(https://developer.apple.com/videos/play/wwdc2019/214/开始于31:13),可以集overrideUserInterfaceStyle到UIUserInterfaceStyleLight或UIUserInterfaceStyleDark任何视图控制器或视图上,它将traitCollection用于任何子视图或视图控制器。
正如SeanR已经提到的,您可以设置UIUserInterfaceStyle到Light或Dark在您的应用程序的plist文件来改变这种为您的整个应用程序。
Aji*_*yak 55
如果您未使用Xcode 11或更高版本(即iOS 13或更高版本的SDK),则您的应用尚未自动选择支持暗模式。因此,无需选择退出黑暗模式。
如果您使用的是Xcode 11或更高版本,则系统已自动为您的应用启用暗模式。有两种方法可以根据您的喜好禁用暗模式。您可以完全禁用它,也可以对任何特定的窗口,视图或视图控制器禁用它。
完全为您的应用禁用暗模式
您可以通过在UIUserInterfaceStyle键中包含与Light应用程序的Info.plist文件中相同的值来禁用暗模式。

这会忽略用户的偏好,并始终为您的应用程序添加浅色外观。
对窗口,视图或视图控制器禁用暗模式
通过设置overrideUserInterfaceStyle适当的窗口,视图或视图控制器的属性,可以强制界面始终以浅色或深色显示。
查看控制器:
override func viewDidLoad() {
super.viewDidLoad()
/* view controller’s views and child view controllers
always adopt a light interface style. */
overrideUserInterfaceStyle = .light
}
Run Code Online (Sandbox Code Playgroud)
观看次数:
// The view and all of its subviews always adopt light style.
youView.overrideUserInterfaceStyle = .light
Run Code Online (Sandbox Code Playgroud)
窗口:
/* 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
Run Code Online (Sandbox Code Playgroud)
注意:Apple强烈建议您在应用中支持暗模式。因此,您只能暂时禁用黑暗模式。
在此处了解更多信息:为iOS应用选择特定的界面样式
waz*_*ski 31
Xcode 12 和 iOS 14 更新。我已经尝试了以前的选项来选择退出暗模式,而 info.plist 文件中的这句话对我不起作用:
<key>UIUserInterfaceStyle</key>
<string>Light</string>
Run Code Online (Sandbox Code Playgroud)
现在它更名为:
<key>Appearance</key>
<string>Light</string>
Run Code Online (Sandbox Code Playgroud)
此设置将阻止完整应用程序中的所有黑暗模式。
编辑:
修正了错字,感谢@sarah
Sea*_*anR 26
我想我已经找到了解决方案。我最初将其从UIUserInterfaceStyle-信息属性列表和UIUserInterfaceStyle-UIKit拼凑而成,但是现在发现它确实记录在为iOS应用选择特定界面样式中。
在中info.plist,将UIUserInterfaceStyle(用户界面样式)设置为1(UIUserInterfaceStyle.light)。
编辑:根据dorbeetle的回答,UIUserInterfaceStyle可能更合适的设置是Light。
Kin*_*ell 22
********** Xcode 11及更高版本的最简单方法***********
将此添加到info.plist之前 </dict></plist>
<key>UIUserInterfaceStyle</key>
<string>Light</string>
Run Code Online (Sandbox Code Playgroud)
Rai*_*kas 18
如果您想退出整个应用程序,则上述答案有效。如果您使用的是具有UI的lib,并且您对.plist的编辑并不奢侈,那么也可以通过代码来实现。
如果您要针对iOS 13 SDK进行编译,则只需使用以下代码即可:
迅速:
if #available(iOS 13.0, *) {
self.overrideUserInterfaceStyle = .light
}
Run Code Online (Sandbox Code Playgroud)
对象C:
if (@available(iOS 13.0, *)) {
self.overrideUserInterfaceStyle = UIUserInterfaceStyleLight;
}
Run Code Online (Sandbox Code Playgroud)
但是,如果您也希望代码也可以针对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")
}
}
Run Code Online (Sandbox Code Playgroud)
对象C:
if (@available(iOS 13.0, *)) {
if ([self respondsToSelector:NSSelectorFromString(@"overrideUserInterfaceStyle")]) {
[self setValue:@(UIUserInterfaceStyleLight) forKey:@"overrideUserInterfaceStyle"];
}
}
Run Code Online (Sandbox Code Playgroud)
Ena*_*que 15
您可以在 Xcode 11 的整个应用程序中关闭暗模式:
添加波纹管
<key>UIUserInterfaceStyle</key>
<string>Light</string>
Run Code Online (Sandbox Code Playgroud)Info.plist 将如下所示...
kum*_*123 12
最新更新-
如果您使用的是Xcode 10.x,则默认UIUserInterfaceStyle值为lightiOS13.x。在iOS 13设备上运行时,它将仅在灯光模式下工作。
无需UIUserInterfaceStyle在Info.plist文件中显式添加密钥,添加密钥将在验证应用程序时出现错误,提示:
无效的Info.plist密钥。Payload / AppName.appInfo.plist文件中的键“ UIUserInterfaceStyle”无效。
仅UIUserInterfaceStyle在使用Xcode 11.x时将密钥添加到Info.plist文件中。
小智 9
iOS 14.3 和 Xcode 12.3 更新
在 info.plist 文件中添加Appearance as Light。
<key>Appearance</key>
<string>Light</string>
Run Code Online (Sandbox Code Playgroud)
如果您将UIUserInterfaceStyle密钥添加到plist文件,则Apple可能会拒绝此处所述的发行版本:https : //stackoverflow.com/a/56546554/7524146
无论如何,明确告诉每个ViewController都是 很烦人的self.overrideUserInterfaceStyle = .light。但是,您可以对根window对象使用一次这种和平的代码:
if #available(iOS 13.0, *) {
if window.responds(to: Selector(("overrideUserInterfaceStyle"))) {
window.setValue(UIUserInterfaceStyle.light.rawValue, forKey: "overrideUserInterfaceStyle")
}
}
Run Code Online (Sandbox Code Playgroud)
只是请注意,您不能在内部执行此操作,application(application: didFinishLaunchingWithOptions:)因为对于该选择器true,它在早期不会响应。但是您以后可以做。如果您在应用程序中使用自定义AppPresenter或AppRouter类,而不是自动在AppDelegate中启动UI,则超级简单。
window!.overrideUserInterfaceStyle = .light
Run Code Online (Sandbox Code Playgroud)
您可以从获取窗口 SceneDelegate
viewController.overrideUserInterfaceStyle = .light
Run Code Online (Sandbox Code Playgroud)
您可以设置任何viewController,甚至连它的viewController内部自我
view.overrideUserInterfaceStyle = .light
Run Code Online (Sandbox Code Playgroud)
您可以设置任何view,甚至里面的观点是自我
if #available(iOS 13.0, *) { ,,, }如果支持较早的iOS版本,则可能需要使用。
斯威夫特 5
从暗模式切换到亮模式的两种方法:
1- info.plist
<key>UIUserInterfaceStyle</key>
<string>Light</string>
Run Code Online (Sandbox Code Playgroud)
2- 以编程方式或运行时
@IBAction private func switchToDark(_ sender: UIButton){
UIApplication.shared.windows.forEach { window in
//here you can switch between the dark and light
window.overrideUserInterfaceStyle = .dark
}
}
Run Code Online (Sandbox Code Playgroud)
我的应用程序目前不支持暗模式并使用浅色应用程序栏颜色。通过将以下键添加到我的中,我能够将状态栏内容强制为深色文本和图标Info.plist:
<key>UIStatusBarStyle</key>
<string>UIStatusBarStyleDarkContent</string>
<key>UIUserInterfaceStyle</key>
<string>Light</string>
<key>UIViewControllerBasedStatusBarAppearance</key>
<true/>
Run Code Online (Sandbox Code Playgroud)
在此处查找其他可能的值:https : //developer.apple.com/documentation/uikit/ustatusbarstyle
颤振用户
不要忘记在 Flutter 应用栏上设置应用栏亮度属性,如下所示:
AppBar(
backgroundColor: Colors.grey[100],
brightness: Brightness.light, // <---------
title: const Text('Hi there'),
),
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
46293 次 |
| 最近记录: |