我的应用程序背景是黑色的,但在iOS 7中,状态栏变成了透明的。所以我什么也看不见,只有角落里的绿色电池指示灯。如何将状态栏文本颜色改为白色,就像在主屏幕上一样?


当前回答

Xcode似乎一直在改变这个,所以这是最新的。

截至2021年,Swift 5, Xcode 12

设置状态栏为白色。

打开你的Info.plist。 添加键UIViewControllerBasedStatusBarAppearance并将值设置为No (false)。人类可读的版本是“基于视图控制器的状态栏外观”。 添加键UIStatusBarStyle和设置值UIStatusBarStyleLightContent(即“轻内容”)。

其他回答

对我来说,什么都没用。我试图改变状态栏颜色在ViewController2,这是嵌入在NavigationController,这反过来,从ViewController1被模态地呈现。这种方法行不通:

override var preferredStatusBarStyle: UIStatusBarStyle {
    return .darkContent
}

什么都没有发生,直到我找到了这个解决方案: 将这一行添加到ViewController1

导航控制器。modalPresentationCapturesStatusBarAppearance = 真正的

let navigationController = UINavigationController(rootViewController: viewController2)
navigationController.modalPresentationStyle = .overFullScreen
navigationController.modalTransitionStyle = .crossDissolve           
navigationController.modalPresentationCapturesStatusBarAppearance = true
self.present(navigationController, animated: true)

所以如果你的导航方案类似于ViewController1呈现的ViewController2,尝试modalPresentationCapturesStatusBarAppearance属性呈现的一个

文档:

The default value of this property is false. When you present a view controller by calling the present(_:animated:completion:) method, status bar appearance control is transferred from the presenting to the presented view controller only if the presented controller's modalPresentationStyle value is UIModalPresentationStyle.fullScreen. By setting this property to true, you specify the presented view controller controls status bar appearance, even though presented non-fullscreen. The system ignores this property’s value for a view controller presented fullscreen.

Xcode似乎一直在改变这个,所以这是最新的。

截至2021年,Swift 5, Xcode 12

设置状态栏为白色。

打开你的Info.plist。 添加键UIViewControllerBasedStatusBarAppearance并将值设置为No (false)。人类可读的版本是“基于视图控制器的状态栏外观”。 添加键UIStatusBarStyle和设置值UIStatusBarStyleLightContent(即“轻内容”)。

这里有一个更好的解决方案 扩展导航控制器并放入故事板

class NVC: UINavigationController {

    override var preferredStatusBarStyle: UIStatusBarStyle {
              return .lightContent
    }

    override func viewDidLoad() {
        super.viewDidLoad()

    self.navigationBar.isHidden = true
    self.navigationController?.navigationBar.isTranslucent = false
      
     self.navigationBar.barTintColor = UIColor.white
     setStatusBarColor(view : self.view)
    }
    

    func setStatusBarColor(view : UIView){
             if #available(iOS 13.0, *) {
                 let app = UIApplication.shared
                 let statusBarHeight: CGFloat = app.statusBarFrame.size.height
                 
                 let statusbarView = UIView()
              statusbarView.backgroundColor = UIColor.black
                 view.addSubview(statusbarView)
               
                 statusbarView.translatesAutoresizingMaskIntoConstraints = false
                 statusbarView.heightAnchor
                     .constraint(equalToConstant: statusBarHeight).isActive = true
                 statusbarView.widthAnchor
                     .constraint(equalTo: view.widthAnchor, multiplier: 1.0).isActive = true
                 statusbarView.topAnchor
                     .constraint(equalTo: view.topAnchor).isActive = true
                 statusbarView.centerXAnchor
                     .constraint(equalTo: view.centerXAnchor).isActive = true
               
             } else {
                 let statusBar = UIApplication.shared.value(forKeyPath: "statusBarWindow.statusBar") as? UIView
              statusBar?.backgroundColor = UIColor.black
             }
         }
}

状态栏颜色将为黑色,文本将为白色

这对我来说真是小菜一碟。

去你的应用的info.plist。

“查看基于控制器的状态栏外观”设置为“否” 设置状态栏样式为UIStatusBarStyleLightContent

然后转到你的应用程序的委托,并粘贴下面的代码,你设置你的窗口的RootViewController。

#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v)  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)

if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0"))
{
    UIView *view=[[UIView alloc] initWithFrame:CGRectMake(0, 0,320, 20)];
    view.backgroundColor=[UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:1.0];
    [self.window.rootViewController.view addSubview:view];
}

宾果。这对我很有用。

您不需要编写任何代码就可以做到这一点! 做以下使状态栏文本颜色白色通过整个应用程序

在你的项目plist文件中:

状态栏样式:透明黑色样式(alpha值为0.5) 基于视图控制器的状态栏外观:NO 状态栏初始隐藏:NO