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

标签 ios swift ios7 statusbar uistatusbar

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

最佳答案

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

  2. viewDidLoad 中执行 [self setNeedsStatusBarAppearanceUpdate];

  3. 添加如下方法:

    - (UIStatusBarStyle)preferredStatusBarStyle
    { 
        return UIStatusBarStyleLightContent; 
    }
    

注意:这不适用于 UINavigationController 内的 Controller ,请参阅 Tyson's comment below :)

Swift 3 - 这将在 UINavigationController 内运行 Controller 。在您的 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 的新 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 - 如何在 iOS 中更改状态栏文本颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17678881/

相关文章:

ios - TableView 为零

ios - 在 iOS7 中后台下载后启动应用程序

ios - 在 ios 7 中如何增加 UINavigationbar 的大小

ios - 如何使我的字体大小在各种设备上看起来不错? UIWebView 中 iPad 和 iPhone 的不同视口(viewport)

ios - 从远程通知启动时如何使用导航堆栈呈现适当的 View Controller

ios - 在用户注销时使 iOS 设备推送 token 无效

ios - Kotlin/Native - 非法尝试访问非共享 <object>

ios - 是否可以在不使用 segue 并仅显示 View Controller 的情况下从 MKAnnotation 传递数据?

html - 带有表情符号的HTML字符串Swift

uitableview - iOS : How to let UITableView draw its cells out of its bounds?