3 回答
TA贡献1784条经验 获得超2个赞
可能的根本原因
我遇到了同样的问题,并且发现它正在发生,因为我没有在我的应用程序窗口中设置根视图控制器。
的UIViewController,其中我已经实现的preferredStatusBarStyle是在一个使用UITabBarController,其控制的屏幕上的意见的外观。
当我将根视图控制器设置为指向此时UITabBarController,状态栏更改开始按预期正常工作(并且该preferredStatusBarStyle方法被调用)。
(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
... // other view controller loading/setup code
self.window.rootViewController = rootTabBarController;
[self.window makeKeyAndVisible];
return YES;}替代方法(在iOS 9中已弃用)
或者,您可以根据需要在每个视图控制器中调用以下方法之一,具体取决于其背景颜色,而不必使用setNeedsStatusBarAppearanceUpdate:
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
要么
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
请注意,您还需要设置UIViewControllerBasedStatusBarAppearance到NO如果您使用此方法在plist文件。
TA贡献1834条经验 获得超8个赞
对于使用UINavigationController的任何人:
该UINavigationController不上转发该preferredStatusBarStyle呼叫到其子视图控制器。相反,它管理自己的状态 - 正如它应该的那样,它绘制在状态栏所在的屏幕顶部,因此应该负责它。因此preferredStatusBarStyle,在导航控制器内的VC中实现将不会做任何事情 - 它们永远不会被调用。
诀窍在于UINavigationController决定返回什么UIStatusBarStyleDefault或者用途的用途UIStatusBarStyleLightContent。它以此为基础UINavigationBar.barStyle。默认(UIBarStyleDefault)会生成暗前景UIStatusBarStyleDefault状态栏。并UIBarStyleBlack会给出一个UIStatusBarStyleLightContent状态栏。
如果你想UIStatusBarStyleLightContent在UINavigationController使用:
self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
TA贡献1111条经验 获得超0个赞
所以我实际上为UINavigationController添加了一个类别,但使用了这些方法:
-(UIViewController *)childViewControllerForStatusBarStyle;-(UIViewController *)childViewControllerForStatusBarHidden;
并让那些返回当前可见的UIViewController。这使得当前可见视图控制器设置其自己的首选样式/可见性。
这是一个完整的代码片段:
在Swift中:
extension UINavigationController {
public override func childViewControllerForStatusBarHidden() -> UIViewController? {
return self.topViewController }
public override func childViewControllerForStatusBarStyle() -> UIViewController? {
return self.topViewController }}在Objective-C中:
@interface UINavigationController (StatusBarStyle)@end@implementation UINavigationController (StatusBarStyle)-(UIViewController *)childViewControllerForStatusBarStyle {
return self.topViewController;}-(UIViewController *)childViewControllerForStatusBarHidden {
return self.topViewController;}@end为了更好的衡量,以下是它在UIViewController中的实现方式:
在斯威夫特
override public func preferredStatusBarStyle() -> UIStatusBarStyle {
return .LightContent}override func prefersStatusBarHidden() -> Bool {
return false}在Objective-C中
-(UIStatusBarStyle)preferredStatusBarStyle {
return UIStatusBarStyleLightContent; // your own style}- (BOOL)prefersStatusBarHidden {
return NO; // your own visibility code}最后,确保你的应用程序plist中不具有“查看基于控制器的状态栏外观”设置为NO。删除该行或将其设置为YES(我认为现在是iOS 7的默认值?)
- 3 回答
- 0 关注
- 1448 浏览
添加回答
举报
