gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 12:23:25 25 4
gpt4 key购买 nike

我有一个容器 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com