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


当前回答

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

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

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

}

结果如下:

其他回答

请试试这个

[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    [application setStatusBarHidden:NO];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    UIView *statusBar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"];
    if ([statusBar respondsToSelector:@selector(setBackgroundColor:)]) {
        statusBar.backgroundColor = [UIColor blackColor];
    }

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

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

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

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

非常简单的方法来改变状态栏的颜色。 创建navigation Controller的子类。

在view didload方法中编写以下代码。 在所有视图控制器中执行此代码

self.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName :
                                                                        [UIColor whiteColor],
                                               NSFontAttributeName:[UIFont boldSystemFontOfSize:19]};

对我来说,什么都没用。我试图改变状态栏颜色在ViewController2,这是嵌入在NavigationController,这反过来,从ViewController1被模态地呈现。这种方法行不通:

override var preferredStatusBarStyle: UIStatusBarStyle {
    return .darkContent
}

什么都没有发生,直到我找到了这个解决方案: 将这一行添加到ViewController1

导航控制器。modalPresentationCapturesStatusBarAppearance = 真正的

let navigationController = UINavigationController(rootViewController: viewController2)
navigationController.modalPresentationStyle = .overFullScreen
navigationController.modalTransitionStyle = .crossDissolve           
navigationController.modalPresentationCapturesStatusBarAppearance = true
self.present(navigationController, animated: true)

所以如果你的导航方案类似于ViewController1呈现的ViewController2,尝试modalPresentationCapturesStatusBarAppearance属性呈现的一个

文档:

The default value of this property is false. When you present a view controller by calling the present(_:animated:completion:) method, status bar appearance control is transferred from the presenting to the presented view controller only if the presented controller's modalPresentationStyle value is UIModalPresentationStyle.fullScreen. By setting this property to true, you specify the presented view controller controls status bar appearance, even though presented non-fullscreen. The system ignores this property’s value for a view controller presented fullscreen.

iOS 7允许单独的视图控制器来决定状态栏的外观,如苹果开发者文档所述:

ios7允许视图控制器在应用程序运行时调整状态栏的样式。动态改变状态栏样式的一个好方法是实现preferredStatusBarStyle并在一个动画块内更新状态栏外观并调用setNeedsStatusBarAppearanceUpdate。

全局设置状态栏外观是一个两步过程。

首先,你需要告诉iOS你不想按视图来设置状态栏外观。

然后你需要负责并实际设置新的全局状态栏样式。

要禁用逐视图状态栏控件,您需要在Info.plist中设置基于视图控制器的状态栏外观属性。

打开项目导航器,选择iOS应用程序的项目,然后选择信息选项卡。

将鼠标悬停在一行上,然后单击出现的加号,将新属性添加到.plist。

在Key字段中输入基于视图控制器的状态栏外观,然后确保Type字段设置为Boolean。最后,在Value字段中输入NO。

要为状态栏设置全局样式,在Info选项卡下添加另一个属性,键为状态栏样式,类型为字符串,值为不透明黑色样式。

下面是一篇博客文章,其中有更多的细节和一些示例代码:

http://codebleep.com/setting-the-status-bar-text-color-in-ios-7/