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


当前回答

如果你想将它设置为任何颜色,请使用下面的代码。

id statusBarWindow = [[UIApplication sharedApplication] valueForKey:@"statusBarWindow"];
id statusBar = [statusBarWindow valueForKey:@"statusBar"];

SEL setForegroundColor_sel = NSSelectorFromString(@"setForegroundColor:");
if([statusBar respondsToSelector:setForegroundColor_sel]) {
    // iOS 7+
    [statusBar performSelector:setForegroundColor_sel withObject:YourColorHere];
                                                                 ^^^^^^^^^^^^^
}

我知道我正在访问私有API,但我已经在许多项目中使用了这个,苹果已经批准了它。

在提交应用程序时,将此代码发送到Apple的评论部分,并告知您正在使用此代码更改状态栏的颜色。

是的,不要忘记下面的内容。

其他回答

只需以下两步:

步骤1:

在项目目标的Info选项卡下,添加行:

UIViewControllerBasedStatusBarAppearance,设置值NO。

步骤2:

在项目AppDelegate.m中:

- (BOOL)application:(UIApplication *)application 
        didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
    …
    [application setStatusBarStyle:UIStatusBarStyleLightContent];
    …
}

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

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

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

}

结果如下:

在信息。plist设置“视图基于控制器的状态栏外观”为NO

在AppDelegate中添加

[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];

你可以在iOS 6和7中使用这个:

#ifdef __IPHONE_7_0
# define STATUS_STYLE UIStatusBarStyleLightContent
#else
# define STATUS_STYLE UIStatusBarStyleBlackTranslucent
#endif

[[UIApplication sharedApplication] setStatusBarStyle:STATUS_STYLE animated:YES];

在信息。plist设置字段值NO查看基于控制器的状态栏外观,并在目标>常规设置中设置状态栏样式灯。