我的应用程序背景是黑色的,但在iOS 7中,状态栏变成了透明的。所以我什么也看不见,只有角落里的绿色电池指示灯。如何将状态栏文本颜色改为白色,就像在主屏幕上一样?
当前回答
在Swift 3非常简单,只需2步。 去你的信息。将基于视图控制器的状态栏外观更改为“NO”。 然后在Appdelegate中在didfinishlaunchingwithoptions方法中添加这一行
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
UIApplication.shared.statusBarStyle = .lightContent
return true
}
这在iOS9中已经被弃用,现在你应该在rootviewcontroller中重写这个属性
这样做在iOS 9中已经被弃用,应该在rootviewcontroller上这样做
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
其他回答
如果你想将它设置为任何颜色,请使用下面的代码。
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的评论部分,并告知您正在使用此代码更改状态栏的颜色。
是的,不要忘记下面的内容。
在AppDelegate。M,添加以下内容。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
}
在Plist文件中,将“基于视图控制器的状态栏外观”设置为NO。
只需以下两步:
步骤1:
在项目目标的Info选项卡下,添加行:
UIViewControllerBasedStatusBarAppearance,设置值NO。
步骤2:
在项目AppDelegate.m中:
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
…
[application setStatusBarStyle:UIStatusBarStyleLightContent];
…
}
对我来说,什么都没用。我试图改变状态栏颜色在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.
实现这一功能的关键是只有全屏视图控制器才能指定状态栏的样式。
如果你正在使用一个导航控制器,并且想要在每个视图控制器的基础上控制状态栏,你会想要子类化UINavigationController并实现preferredStatusBarStyle,这样它就会返回topViewController的首选项。
确保你将故事板场景中的类引用从muinavigationcontroller更改为你的子类(例如下面例子中的MyNavigationController)。
(以下方法对我有用。如果你的应用程序是基于TabBar,你会想做类似的事情通过子类化UITabBarController,但我还没有尝试出来)。
@interface MyNavigationController : UINavigationController
@end
@implementation MyNavigationController
- (UIStatusBarStyle)preferredStatusBarStyle
{
return self.topViewController.preferredStatusBarStyle;
}
@end
推荐文章
- 为什么Swift的编译时间这么慢?
- iPhone上UIView和UILabels的渐变
- 如何测试等价的Swift枚举与相关的值
- keychain上的分发证书中缺少私钥
- 在实现API时,我如何避免在块中捕获自我?
- 如何创建一个Swift Date对象?
- 在Swift中转换Float为Int
- Xcode 4在目标设备上说“finished running <my app>”——什么都没有发生
- 从另一个应用程序打开设置应用程序
- 快速提取正则表达式匹配
- 如何应用梯度的背景视图的iOS Swift应用程序
- 我如何在Swift中声明一个弱引用数组?
- 图书馆吗?静态的?动态吗?或框架?另一个项目中的项目
- 如何用SwiftUI调整图像大小?
- Xcode 6 gitignore文件应该包括什么?