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


当前回答

这个解决方案适用于使用新的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”。

其他回答

如果我使用UINavigationController,我在iOS 9和Swift 2.0中做这个

self.navigationController?.navigationBar.barStyle = UIBarStyle.Black

如果我使用模态segue

override func preferredStatusBarStyle() -> UIStatusBarStyle {
    return .LightContent
}

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

-[UINavigationBar setBarStyle:UIBarStyleBlack]

原始答案在这里

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

在Swift 5的例子中,我添加了这些行:

override func viewDidAppear(_ animated: Bool) {
    navigationController?.navigationBar.barStyle = .black
}

override var preferredStatusBarStyle: UIStatusBarStyle {
    return .lightContent
}

如果你有一个通过Interface Builder创建的嵌入式导航控制器,请确保在管理导航控制器的类中设置以下内容:

-(UIStatusBarStyle)preferredStatusBarStyle{ 
    return UIStatusBarStyleLightContent; 
} 

这应该就是你所需要的。

对于Xcode 5.1:

在.plist的NO中添加“基于视图控制器的状态栏外观”。

在AppDelegate中添加:

[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];

这是所有!