如何在 iOS 中更改状态栏文本颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17678881/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to change Status Bar text color in iOS
提问by Oleksandr Veremchuk
My application has a dark background, but in iOS 7 the status bar became transparent. So I can't see anything there, only the green battery indicator in the corner. How can I change the status bar text color to white like it is on the home screen?
我的应用程序有一个黑暗的背景,但在 iOS 7 状态栏变得透明。所以我在那里什么也看不到,只有角落里的绿色电池指示灯。如何像在主屏幕上一样将状态栏文本颜色更改为白色?
回答by Peter B. Kramer
Set the
UIViewControllerBasedStatusBarAppearance
toYES
in the .plist file.In the
viewDidLoad
do a[self setNeedsStatusBarAppearanceUpdate];
Add the following method:
- (UIStatusBarStyle)preferredStatusBarStyle { return UIStatusBarStyleLightContent; }
在 .plist 文件中设置
UIViewControllerBasedStatusBarAppearance
toYES
。在
viewDidLoad
做一个[self setNeedsStatusBarAppearanceUpdate];
添加以下方法:
- (UIStatusBarStyle)preferredStatusBarStyle { return UIStatusBarStyleLightContent; }
Note: This does not work for controllers inside UINavigationController
, please see Tyson's comment below:)
注意:这不适用于内部控制器UINavigationController
,请参阅下面泰森的评论:)
Swift 3- This will work controllers inside UINavigationController
. Add this code inside your controller.
Swift 3- 这将在UINavigationController
. 将此代码添加到您的控制器中。
// Preferred status bar style lightContent to use on dark background.
// Swift 3
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
Swift 5 and SwiftUI
Swift 5 和 SwiftUI
For SwiftUI create a new swift file called HostingController.swift
对于 SwiftUI,创建一个名为的新 swift 文件 HostingController.swift
import Foundation
import UIKit
import SwiftUI
class HostingController: UIHostingController<ContentView> {
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
}
Then change the following lines of code in the SceneDelegate.swift
然后更改以下代码行 SceneDelegate.swift
window.rootViewController = UIHostingController(rootView: ContentView())
to
到
window.rootViewController = HostingController(rootView: ContentView())
回答by wilsontgh
Alternatively, you can opt out of the view-controller based status bar appearance:
或者,您可以选择退出基于视图控制器的状态栏外观:
- Set
View controller-based status bar appearance
toNO
in yourInfo.plist
. - Call
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
- 设置
View controller-based status bar appearance
到NO
你Info.plist
。 - 称呼
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
Note: This method has been deprecated in iOS9. Use preferredStatusBarStyle
on the UIViewController instead. (see Apple Developer Library)
注意:此方法在 iOS9 中已被弃用。preferredStatusBarStyle
改为在 UIViewController 上使用。(请参阅Apple 开发人员库)
回答by Lucas
You can do this without writing any line of code!
Do the following to make the status bar text color white through the whole app
您无需编写任何代码行即可完成此操作!
执行以下操作使整个应用程序的状态栏文本颜色为白色
On you project plistfile:
在您的项目 plist文件中:
- Status bar style:
Transparent black style (alpha of 0.5)
- View controller-based status bar appearance:
NO
- Status bar is initially hidden:
NO
- 状态栏样式:
Transparent black style (alpha of 0.5)
- 查看基于控制器的状态栏外观:
NO
- 状态栏最初是隐藏的:
NO
回答by Pradeep Mahdevu
Note: Most upvoted answer does not work for iOS 7 / 8
注意:大多数赞成的答案不适用于 iOS 7 / 8
In Info.plist set 'View controller-based status bar appearance' as NO
在 Info.plist 中将“基于控制器的状态栏外观”设置为 NO
In AppDelegate add
在 AppDelegate 添加
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
to
到
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
......
...
}
This solution works for iOS 7 / 8
此解决方案适用于 iOS 7 / 8
回答by haraldmartin
For me, nothing happened with using all the things in the other answers (and from other sources/documentation). What did help was to set the Navigation Bar Style to "Black" in the XIB. This changed the text to white without any code at all.
对我来说,使用其他答案(以及其他来源/文档)中的所有内容都没有发生任何事情。有帮助的是在 XIB 中将导航栏样式设置为“黑色”。这将文本更改为白色,根本没有任何代码。
回答by stepik21
None of that worked for me, so here is a working solution...
这些都不适合我,所以这里有一个可行的解决方案......
In Info.plist
, add a row:
在 中Info.plist
,添加一行:
UIViewControllerBasedStatusBarAppearance
, and set the value NO
.
UIViewControllerBasedStatusBarAppearance
,并设置值NO
。
Then in AppDelegate in didFinishLaunchingWithOptions
, add these rows:
然后在 AppDelegate 中didFinishLaunchingWithOptions
,添加以下行:
[application setStatusBarHidden:NO];
[application setStatusBarStyle:UIStatusBarStyleLightContent];
回答by Ajinkya Patil
You dont need to do any code for this
你不需要为此做任何代码
You need to add "View controller-based status bar appearance" key in info.plist as follows:
您需要在 info.plist 中添加“基于视图控制器的状态栏外观”键,如下所示:
& set its value type to Boolean & value to NO. Then click on project settings,then click on General Tab & under Deployment Info set the preferred status bar style to .Light as follows:
& 将其值类型设置为布尔值 & 值设置为 NO。然后单击项目设置,然后单击常规选项卡 & 在部署信息下将首选状态栏样式设置为 .Light,如下所示:
Thats it.
就是这样。
回答by folse
Just two steps as following:
只需以下两步:
Step 1:
第1步:
Under the Info tab of the project target
, Add Row:
在 的信息选项卡下project target
,添加行:
UIViewControllerBasedStatusBarAppearance
, set value NO
.
UIViewControllerBasedStatusBarAppearance
,设定值NO
。
Step 2:
第2步:
In the project AppDelegate.m
:
在项目中AppDelegate.m
:
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
…
[application setStatusBarStyle:UIStatusBarStyleLightContent];
…
}
回答by Deniss Fedotovs
This works in Golden Master iOS 7 and Xcode 5 GM seed and iOS7 SDK released on September 18th, 2013 (at least with navigation controller hidden):
这适用于 2013 年 9 月 18 日发布的 Golden Master iOS 7 和 Xcode 5 GM 种子和 iOS7 SDK(至少隐藏了导航控制器):
Set
the UIViewControllerBasedStatusBarAppearance
toNO
in theInfo.plist
.In
ViewDidLoad
method or anywhere, where do you want to change status bar style:[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
设置
the UIViewControllerBasedStatusBarAppearance
到NO
在Info.plist
。在
ViewDidLoad
方法或任何地方,您想在哪里更改状态栏样式:[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
回答by Mohit Padalia
In case your UIViewController is inside a UINavigationController you will have to set the BarStyle:
如果您的 UIViewController 在 UINavigationController 内,您将必须设置 BarStyle:
-[UINavigationBar setBarStyle:UIBarStyleBlack]
Original Answer is here
原答案在这里