如何在 iOS 中更改状态栏文本颜色 [英] How to change Status Bar text color in iOS

查看:33
本文介绍了如何在 iOS 中更改状态栏文本颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

推荐答案

  1. 在 .plist 文件中将 UIViewControllerBasedStatusBarAppearance 设置为 YES.

viewDidLoad中做一个[self setNeedsStatusBarAppearanceUpdate];

添加以下方法:

- (UIStatusBarStyle)preferredStatusBarStyle
{ 
    return UIStatusBarStyleLightContent; 
}

注意:这不适用于 UINavigationController 内部的控制器,请参阅 泰森在下面的评论 :)

Note: This does not work for controllers inside UINavigationController, please see Tyson's comment below :)

Swift 3 - 这将在 UINavigationController 中运行控制器.将此代码添加到您的控制器中.

Swift 3 - This will work controllers inside 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 和 SwiftUI

为 SwiftUI 创建一个名为 HostingController.swift

For SwiftUI create a new swift file called HostingController.swift

import Foundation
import UIKit
import SwiftUI

class HostingController: UIHostingController<ContentView> {
    override var preferredStatusBarStyle: UIStatusBarStyle {
        return .lightContent
    }
}

然后在SceneDelegate.swift

window.rootViewController = UIHostingController(rootView: ContentView())

window.rootViewController = HostingController(rootView: ContentView())

这篇关于如何在 iOS 中更改状态栏文本颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆