如何在更改初始视图后加载状态栏
问题描述:
显示我的ArticlesViewController时出现问题。这段代码很好用,但是当Articles页面出现时,没有状态栏。 我觉得有什么毛病导航控制器,所以我试图添加如何在更改初始视图后加载状态栏
[UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
无论是在AppDelegate中的didFinishLaunchingWithOptions和ArticlesViewController的viewDidLoad中和viewWillAppear中,但仍然没有结果
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
NSManagedObjectContext *context = [self managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Entity"];
localStorage = [[context executeFetchRequest:fetchRequest error:nil] mutableCopy];
NSObject *obj = [localStorage valueForKey:@"isAppLaunchedOnce"];
NSString *condition = [[[obj description] componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] componentsJoinedByString:@""];
NSCharacterSet *unwantedChars = [NSCharacterSet characterSetWithCharactersInString:@"\"()"];
condition = [[condition componentsSeparatedByCharactersInSet:unwantedChars] componentsJoinedByString:@""];
if ([condition isEqual: @"YES"]){
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UIViewController *rootViewController = (ArticlesViewController*)[storyboard instantiateViewControllerWithIdentifier:@"ArticlesViewController"];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:rootViewController];
self.window.rootViewController = rootViewController;
if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_6_1){
navController.navigationBar.tintColor = [UIColor whiteColor];
navController.navigationItem.titleView.tintColor = [UIColor whiteColor];
NSDictionary *titleAttributes = @{
NSFontAttributeName:[UIFont fontWithName:@"Helvetica-Bold" size:14.0],
NSForegroundColorAttributeName:[UIColor whiteColor]
};
navController.navigationBar.titleTextAttributes = titleAttributes;
}
[self.window makeKeyAndVisible];
}
// Override point for customization after application launch.
return YES;
}
答
在ArticlesViewController
中加入下面的方法试试看。
- (BOOL)prefersStatusBarHidden
{
return NO;
}
如果你没有在plist中设置,那么你需要为每个控制器设置。
您也可以仅仅通过增加以下线didFinishLaunchingWithOptions:
[[UIApplication sharedApplication] setStatusBarHidden:NO];
检查这些关键的plist文件和设置状态栏最初还隐藏着如NO
设置
如果您有主要故事板,为什么要在代码中创建导航控制器和文章视图控制器? – matt
@matt据我了解,我不创建ArticlesViewController,我从故事板获得这个控制器,然后改变我的initialViewController在应用程序。如果你认为我可以使用我现有的导航控制器 - 问我如何,它可能是一个答案 – dimazava