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


当前回答

这似乎是当前Xcode和iOS 7的一个问题。

在苹果开发者论坛*的“iOS 7 Beta Livability”中搜索UIStatusBarStyleLightContent(目前有32个帖子),可以看到苹果开发者论坛上的一些相关内容。

我在把它调到浅色的时候偶然发现的。

(这只是对Aaron回答的一个补充。)

其他回答

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

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

也可能令人感兴趣:

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

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

以下是苹果关于状态栏更改的指导方针/说明。状态栏只允许显示暗&亮(而&黑)。

这里是-如何改变状态栏样式:

如果你想设置状态栏的风格,应用程序级别,然后设置UIViewControllerBasedStatusBarAppearance为NO在你的。plist文件。

如果你想在视图控制器级别设置状态栏样式,那么按照以下步骤:

如果你只需要在UIViewController级别设置状态栏样式,在.plist文件中将UIViewControllerBasedStatusBarAppearance设置为YES。 在viewDidLoad中添加函数setNeedsStatusBarAppearanceUpdate 重写视图控制器中的preferredStatusBarStyle。

-

override func viewDidLoad() {
    super.viewDidLoad()
    self.setNeedsStatusBarAppearanceUpdate()
}

override var preferredStatusBarStyle: UIStatusBarStyle {
    return .lightContent
}

根据状态栏样式设置级别设置.plist的值。

下面是一些在应用程序启动或视图控制器viewDidLoad期间更改/设置状态栏背景色的技巧。

extension UIApplication {

    var statusBarView: UIView? {
        return value(forKey: "statusBar") as? UIView
    }

}

// Set upon application launch, if you've application based status bar
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        UIApplication.shared.statusBarView?.backgroundColor = UIColor.red
        return true
    }
}


or 
// Set it from your view controller if you've view controller based statusbar
class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        UIApplication.shared.statusBarView?.backgroundColor = UIColor.red
    }

}

结果如下:

如果你的UIViewController在UINavigationController中,你必须设置BarStyle:

-[UINavigationBar setBarStyle:UIBarStyleBlack]

原始答案在这里

https://devforums.apple.com/message/844264#844264

extension UIApplication {

    var statusBarView: UIView? {
        return value(forKey: "statusBar") as? UIView
    }
}

这个解决方案适用于使用新的SwiftUI Lifecycle / iOS 14.0的应用程序:

我需要动态更改状态栏文本颜色,无法访问窗口。rootViewController,因为SwiftUI生命周期中不存在SceneDelegate。

我终于找到了Xavier Donnellon的简单解决方案:https://github.com/xavierdonnellon/swiftui-statusbarstyle

复制StatusBarController.swift文件到你的项目中,并将你的主视图包装成一个RootView:

@main
struct ProjectApp: App {     
    var body: some Scene {
        WindowGroup {
            //wrap main view in RootView
            RootView {
                //Put the view you want your app to present here
                ContentView()
                    //add necessary environment objects here 
            }
        }
    }
}

然后你可以通过使用。statusbarstyle (. darkcontent)或。statusbarstyle (. lightcontent)视图修饰符来改变状态栏文本的颜色,或者直接调用例如UIApplication.setStatusBarStyle(. lightcontent)。

不要忘记在Info.plist中设置“基于视图控制器的状态栏外观”为“YES”。