如何在iOS中更改状态栏文本颜色

本文翻译自:How to change Status Bar text color in iOS

My application has a dark background, but in iOS 7 the status bar became transparent. 我的应用程序具有深色背景,但是在iOS 7中状态栏变为透明的。 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? 如何将状态栏文本的颜色更改为白色,就像在主屏幕上一样?


#1楼

参考:https://stackoom.com/question/1CB5F/如何在iOS中更改状态栏文本颜色


#2楼

This is documented in the iOS 7 UI Transition Guide , which you need an Apple developer ID to access directly. 这在《 iOS 7 UI过渡指南》中有所记录,您需要Apple开发人员ID才能直接访问。 The relevant excerpt: 相关摘录:

Because the status bar is transparent, the view behind it shows through. 由于状态栏是透明的,因此它后面的视图将显示出来。 [...] Use a UIStatusBarStyle constant to specify whether the statusbar content should be dark or light: [...]使用UIStatusBarStyle常量来指定状态栏内容应为深色还是浅色:

UIStatusBarStyleDefault displays dark content. UIStatusBarStyleDefault显示暗的内容。 [...] [...]

UIStatusBarStyleLightContent displays light content. UIStatusBarStyleLightContent显示灯光内容。 Use when dark content is behind the status bar. 当状态栏后面有深色内容时使用。

Also possibly of interest: 也可能感兴趣:

In iOS 7, you can control the style of the status bar from an individual vew controller and change it while the app runs. 在iOS 7中,您可以从单个vew控制器控制状态栏的样式,并在应用运行时对其进行更改。 To opt in to this behavior, add the UIViewControllerBasedStatusBarAppearance key to an app's Info.plist file and give it the value YES . 要选择这种行为,请将UIViewControllerBasedStatusBarAppearance键添加到应用程序的Info.plist文件中,并将其值设置为YES

I'd definitely recommend having a look through the document, which, again, you can access with your Apple developer ID. 我绝对建议您浏览一下该文档,同样,您可以使用Apple开发人员ID进行访问。


#3楼

  1. Set the UIViewControllerBasedStatusBarAppearance to YES in the .plist file. 在.plist文件中,将UIViewControllerBasedStatusBarAppearance设置为YES

  2. In the viewDidLoad do a [self setNeedsStatusBarAppearanceUpdate]; viewDidLoad执行[self setNeedsStatusBarAppearanceUpdate];

  3. Add the following method: 添加以下方法:

     - (UIStatusBarStyle)preferredStatusBarStyle { return UIStatusBarStyleLightContent; } 

Note : This does not work for controllers inside UINavigationController , please see Tyson's comment below :) 注意 :这不适用于UINavigationController内部的控制器,请参见下面的Tyson评论 :)

Swift 3 - This will work controllers inside UINavigationController . Swift 3-这将在UINavigationController内部工作控制器。 Add this code inside your controller. 将此代码添加到您的控制器中。

// 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,创建一个名为HostingController.swift的新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())

#4楼

Alternatively, you can opt out of the view-controller based status bar appearance: 或者,您可以选择不使用基于视图控制器的状态栏外观:

  1. Set View controller-based status bar appearance to NO in your Info.plist . Info.plist View controller-based status bar appearanceNO
  2. Call [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent]; 调用[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];

Note: This method has been deprecated in iOS9. 注意:此方法已在iOS9中弃用。 Use preferredStatusBarStyle on the UIViewController instead. 请改用UIViewController上的preferredStatusBarStyle (see Apple Developer Library ) (请参阅Apple Developer Library


#5楼

If you have an embedded navigation controller created via Interface Builder , be sure to set the following in a class that manages your navigation controller: 如果您具有通过Interface Builder创建的嵌入式导航控制器,请确保在管理导航控制器的类中设置以下内容:

-(UIStatusBarStyle)preferredStatusBarStyle{ 
    return UIStatusBarStyleLightContent; 
} 

That should be all you need. 那应该是您所需要的。


#6楼

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 plist file: 在您的项目plist文件上:

  • Status bar style: Transparent black style (alpha of 0.5) 状态栏样式: Transparent black style (alpha of 0.5)
  • View controller-based status bar appearance: NO 基于视图控制器的状态栏外观: NO
  • Status bar is initially hidden: NO 状态栏最初是隐藏的: NO
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值