iOS 7 UIStatusBarStyle在应用程序启动期间隐藏状态栏的问题

5
我正在更新我的应用程序,将导航栏的颜色从白色改为蓝色。因此,我想将状态栏的颜色从黑色更改为白色。我已经尝试了苹果文档中的所有方法,这些方法帮助我更改了我的Storyboard中所有视图控制器的状态栏样式。
然而,任何通过presentViewController推送到不在我的Storyboard中的nib都会自动更改UIStatusBarStyle - 这是我的推送代码:
NSString *url = @"https://twitter.com/Example";
                NSString *title = @"Example";
SocialWebViewController *addController = [[[SocialWebViewController alloc] initWithURL:url title:title] initWithNibName:@"SocialWebView_iPhone" bundle:nil];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:addController];
[self presentViewController:navigationController animated:YES completion:nil];

我已经在我的AppDelegate中设置了UIStatusBar的首选样式,这个方法是有效的,但仅适用于我的StoryBoard中的ViewControllers:

- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleLightContent;
}

是的,我已经尝试过以下方法:

(1) [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];

(2) [self setNeedsStatusBarAppearanceUpdate];

(3) 在plist中将UIViewControllerBasedStatusBarAppearance设置为YES

但以上方法都不能解决没有在我的StoryBoard中的Xib文件的问题。

我找到了原因 - 当我将UIViewControllerBasedStatusBarAppearance设置为“NO”时,在应用程序启动期间我的状态栏最初被隐藏,这导致无论何时我都不能显示它... 我该如何解决?


2
UIViewControllerBasedStatusBarAppearance 应该不是 NO 吗?这样就不会自动更改了。 - erdekhayser
1
这不是错误,而是特性。 - Toseef Khilji
2个回答

7
在您的*-Info.plist文件中设置以下键:
UIStatusBarStyle = UIStatusBarStyleLightContent
UIViewControllerBasedStatusBarAppearance = NO
UIStatusBarHidden = YES

在您的 AppDelegate 中,将以下行添加到方法 application:didFinishLaunchingWithOptions 中:

[[UIApplication sharedApplication] setStatusBarHidden:NO];

1
是的,这就是楼主正在寻找的解决方案。 - erdekhayser
将info.plist中的“状态栏样式”设置为某个值并不会产生任何效果。此外,将“视图控制器基于状态栏外观”的值设置为“NO”或“False”只会隐藏状态栏。 - Bobi
@user2981416 你清理了项目吗?我现在正在一个简单的项目中测试,一切都运行正常。 - LuisEspinoza
是的,我已经清理了这个项目。很奇怪。您是在使用NavigationControllers或nibs的Storyboard进行测试吗?因为对于我的所有ViewControllers都在Storyboard中工作正常,但不适用于我使用presentViewController推送的nibs。也许您可以尝试一下MailComposer?看看当它打开一个草稿消息时会发生什么。(如果您不知道如何操作,请告诉我,我可以提供使用MailComposer的代码) - Bobi
对于为什么在应用程序启动时隐藏状态栏会搞乱一切感到困惑。顺便说一下,谢谢你的帮助。 - Bobi
显示剩余2条评论

0
如果您想在整个应用程序中将状态栏的样式设置为浅色,最简单的方法是将UIViewControllerBasedStatusBarAppearance设置为NO,然后执行以下操作:
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
否则,仅在UIViewController中设置preferredStatusBarStyle应该可以正常工作,无需进行其他修改。
- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleLightContent;
}

我怀疑您正在使用UINavigationController/UITabBarController等来显示UIViewController。如果是这种情况,那么您需要子类化UINavigationController/UITabBarController以添加正确的状态栏样式,您需要在这些控制器中添加preferredStatusBarStyle方法。这样做的简单实现如下:

UINavigationControllerSubclass.h

#import <UIKit/UIKit.h>

@interface UINavigationControllerSubclass : UINavigationController

@end

UINavigationControllerSubclass.m

#import "UINavigationControllerSubclass.h"

@interface UINavigationControllerSubclass ()

@end

@implementation UINavigationControllerSubclass

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleLightContent;
}

@end

只需使用它,而不是UINavigationController,状态栏现在应该是浅色的。

处理此类UI问题时,您始终需要考虑控制器堆栈。由于UINavigationController包含UIViewController,因此如果您仅在UIViewController中设置状态栏属性,UINavigationController将不知道它并保持其原始属性。这就是为什么创建UINavigationController子类并在那里设置状态栏样式可以解决所有问题。如果您遇到此类问题,请尝试记住这一点。


嗨,我编辑了我的问题,因为我刚刚注意到当我将UIViewControllerBasedStatusBarAppearance设置为NO时,它为什么对我不起作用。 当我将“在应用程序启动期间隐藏状态栏”更改为YES(这是我想要发生的事情)时,会导致状态栏一直被隐藏。 但是当我将其更改为NO时,就像你们一直说的那样,状态栏颜色似乎是白色的...我该如何解决这个问题? - Bobi
如果您想要黑色文本,将statusBarStyle设置为暗色而不是亮色。 - Pete42
我实际上想让它变成白色,但不想必须将“应用程序启动期间隐藏状态栏”设置为“是”才能使所有内容正常工作...? - Bobi
这就是我的问题所在 - 在UIViewControllers中实现preferredStatusBarStyle和/或在AppDelegate中使用'-(UIStatusBarStyle)preferredStatusBarStyle {return UIStatusBarStyleLightContent;}'除非我将View controller-based status bar appearance设置为NO,否则不起作用,如果我这样做,那么我的状态栏就会被隐藏,除非我在应用程序启动期间隐藏状态栏,这让我很疯狂。我还注意到,无论我做什么,状态栏在应用程序启动时和使用MailComposer时的颜色都是错误的。 - Bobi
我已经编辑了我的答案,以支持最初的隐藏行为。 - LuisEspinoza
显示剩余3条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接