ios - 转换到新的 View Controller 时如何更新状态栏样式?

标签 ios cocoa-touch uistatusbar

我有一个容器 View Controller ,它使用以下代码从一个 subview Controller 转换到另一个 subview Controller :

- (void)switchToNewViewController:(UIViewController *)newController {
  [self 
    transitionFromViewController:self.currentViewController 
    toViewController:newController
    duration: 0.6
    options:UIViewAnimationOptionTransitionFlipFromLeft
    animations:^{
      newController.view.frame = self.currentViewController.view.frame;
    }
    completion:^(BOOL finished) {
      [self.currentViewController removeFromParentViewController];
      [newController didMoveToParentViewController:self];
      self.currentViewController = newController;
    }
  ];
}

- (UIViewController*)childViewControllerForStatusBarStyle
{
  return self.currentViewController;
}

当第一个 View Controller 更喜欢状态栏中的深色文本但下一个 View Controller 更喜欢浅色文本时,我遇到了一个问题。

第一个 View Controller :

- (UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleDefault;
}

第二个 View Controller :

- (UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleLightContent;
}

我在第一个和第二个 View Controller 的 preferredStatusBarStyle 中都放置了一个断点。调试器只在第一个 View Controller 上中断,但不会在第二个 View Controller 上中断。因此,状态栏样式不会改变。我如何通知 Cocoa 框架它需要读取第二个 View Controller 的首选项?

最佳答案

确保 UIViewControllerBasedStatusBarAppearance 在您的 Info.plist 文件中设置为 YES。这应该确保每个 View Controller 调用 preferredStatusBarStyle

否则,将上面的 plist 键设置为 NO,然后将以下内容添加到每个 UIViewController 的实现文件中的 viewWillAppear:

if ([self respondsToSelector:@selector(setNeedsStatusBarAppearanceUpdate)]) { [自行设置NeedsStatusBarAppearanceUpdate]; }

您需要确保在每个需要更新状态栏外观的 View Controller 中实现这一点。

如果一切都失败了

从任何你想更新状态栏样式的地方调用以下:

[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];

显然用您想要的状态栏样式替换样式。

希望这对您有所帮助!

关于ios - 转换到新的 View Controller 时如何更新状态栏样式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29907436/

相关文章:

objective-c - 应用程序在后台时不显示 iOS UILocalNotification

ios - 状态栏图标消失然后慢慢重新出现

ios - 用自己的 View 替换状态栏

iphone - 这是解析iPhone中RSS提要的最佳方法

iOS:升级 iPad 应用程序的步骤是什么?

ios - isKindOfClass 莫名其妙的身份问题

iOS 8 使状态栏变黑而不是半透明

objective-c - 为什么我的NSArray被释放?

ios - 如何在 UILabel 中设置 2 行属性字符串

objective-c - 有没有办法删除日期选择器的 "year"?