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


当前回答

这个答案是在hackingwithswift网站的帮助下得出的

用于iOS (13, *)

有时候我们需要不同颜色的状态栏,例如对于一个ViewController我们需要黑色的状态栏,而对于第二个ViewController我们需要白色的状态栏。 现在我们要做什么? 我们需要在ViewController中添加这个和平代码

    // MARK: - Variables
    override var preferredStatusBarStyle: UIStatusBarStyle {
        return .lightContent
    }
    // MARK: - View Life Cycle
    override func viewDidAppear(_ animated: Bool) {
        setNeedsStatusBarAppearanceUpdate()
    }

这段代码将改变特定ViewController中状态栏的浅色或白色。我们可以在preferredStatusBarStyle中将其更改为。dark

欲了解更多细节,请访问hackingwithswift

其他回答

这在iOS 7 UI过渡指南中有记录,你需要一个苹果开发者ID才能直接访问。相关节选:

因为状态栏是透明的,所以它后面的视图是透视的。[…使用UIStatusBarStyle常量来指定状态栏内容应该是暗的还是亮的: UIStatusBarStyleDefault显示黑色内容。[…] UIStatusBarStyleLightContent显示轻内容。当状态栏后面是黑色内容时使用。

也可能令人感兴趣:

在iOS 7中,你可以通过单个的vew控制器控制状态栏的样式,并在应用程序运行时更改它。要选择这种行为,添加UIViewControllerBasedStatusBarAppearance键到应用程序的信息。plist文件并赋值YES。

我强烈建议你浏览一下这个文档,同样,你可以用你的苹果开发者ID访问这个文档。

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

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
             }
         }
}

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

你可以在iOS 6和7中使用这个:

#ifdef __IPHONE_7_0
# define STATUS_STYLE UIStatusBarStyleLightContent
#else
# define STATUS_STYLE UIStatusBarStyleBlackTranslucent
#endif

[[UIApplication sharedApplication] setStatusBarStyle:STATUS_STYLE animated:YES];

改变所有viewcontroller的状态栏文本颜色

斯威夫特3

if在Info.plist中查看基于控制器的状态栏外观= YES

然后使用这个扩展为所有NavigationController

extension UINavigationController
{
    override open var preferredStatusBarStyle: UIStatusBarStyle {
    return .lightContent
     }
 }

如果没有UINavigationController,只有UIViewController,那么使用下面的代码:

extension UIViewController
{
    override open var preferredStatusBarStyle: UIStatusBarStyle {
    return .lightContent
     }
 }

objective - c

创建类别类

对于UIView控制器

在ui + StatusBarStyle.h

 @interface UIViewController (StatusBarStyle)
 @end

在ui + StatusBarStyle.m

 #import "UIViewController+StatusBarStyle.h"

 @implementation UIViewController (StatusBarStyle)
 -(UIStatusBarStyle)preferredStatusBarStyle
 {
  return UIStatusBarStyleLightContent;
 }
 @end 

对于UINavigationController

在UINavigationController + StatusBarStyle.h

 @interface UINavigationController (StatusBarStyle)
 @end

在UINavigationController + StatusBarStyle.m

 #import "UINavigationController+StatusBarStyle.h"

 @implementation UINavigationController (StatusBarStyle)
 -(UIStatusBarStyle)preferredStatusBarStyle
 {
  return UIStatusBarStyleLightContent;
 }
 @end  

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

去你的应用的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];
}

宾果。这对我很有用。