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


当前回答

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

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

override var preferredStatusBarStyle: UIStatusBarStyle {
    return .lightContent
}

其他回答

这适用于Golden Master iOS7和Xcode 5 GM种子和2013年9月18日发布的iOS7 SDK(至少导航控制器隐藏):

中的UIViewControllerBasedStatusBarAppearance设置为NO Info.plist。 在ViewDidLoad方法或任何地方,你想改变的地方 状态栏样式: [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];

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

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

如果我使用模态segue

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

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

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

宾果。这对我很有用。

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

-[UINavigationBar setBarStyle:UIBarStyleBlack]

原始答案在这里

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

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

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

如果你想设置状态栏的风格,应用程序级别,然后设置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
    }

}

结果如下: