【问题标题】:How to use modalPresentationCapturesStatusBarAppearance = NO with a custom UIPresentationController?如何将 modalPresentationCapturesStatusBarAppearance = NO 与自定义 UIPresentationController 一起使用?
【发布时间】:2015-10-18 02:25:00
【问题描述】:

我有一个自定义 UIPresentationController 并覆盖 frameOfPresentedViewInContainerView 以进行自定义 viewController 演示。一切正常,除了状态栏。

我根本不希望状态栏改变外观——它应该保持原来的样子。现在 Apple 文档:https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIViewController_Class/#//apple_ref/occ/instp/UIViewController/modalPresentationCapturesStatusBarAppearance 说如果 modalPresentationStyle 不是 UIModalPresentationFullScreen 或 modalPresentationCapturesStatusBarAppearance 是 NO,我应该没问题,状态栏不应该改变。

使用此代码:

- (BOOL)prefersStatusBarHidden {
    NSLog(
        @"prefersStatusBarHidden was called %d %ld",
        self.modalPresentationCapturesStatusBarAppearance,
        (long)self.modalPresentationStyle
    );

    return YES;
}

我可以看到调用了 prefersStatusBarHidden,即使 modalPresentationCapturesStatusBarAppearance 为 NO(显示为 0)并且 modalPresentationStyle 为 UIModalPresentationCustom(显示为 4)。

显然,这就是在呈现 viewController 时状态栏发生变化的原因。

但是为什么呢?

我的想法是 iOS 认为 viewController 是全屏显示的,即使它不是。

我发现了 UIPresentationController 的属性 shouldPresentInFullscreen——它默认返回 YES。返回 NO 根本没有帮助,所以我不明白该属性甚至做了什么......它实际上没有任何效果。这同样适用于presentationStyle 属性——更改它时我看不到任何效果。我本来希望presentationStyle 属性被“重定向”到viewControllers modalPresentationStyle 属性,但它停留在UIModalPresentationCustom,它必须首先启动自定义演示。

所以,我的问题是:有人知道如何使用自定义 UIPresentationController 保持状态栏不变 - 有人可以解释一下 shouldPresentInFullscreen 和presentationStyle 属性吗?

谢谢! :)

【问题讨论】:

  • 你有没有解决过这些问题?我还试图让 UIPresentationController 不显示全屏(实际上我希望能够与一些底层视图元素进行交互——比如标签栏)。我不知道如何让它给我一个不是窗口根视图控制器的presentingViewController。
  • 不,遗憾的是我从来没有弄清楚:(
  • 看起来这在 iOS 10 中已修复。我在这里为 iOS 9 提出了一个骇人听闻的解决方案:stackoverflow.com/questions/34819013/…

标签: ios uiviewcontroller uistatusbar uimodalpresentationstyle uipresentationcontroller


【解决方案1】:

尝试实现 childViewControllerForStatusBarStyle: 并在调用 UIPresentationController 的类中为它返回 nil,通常是 UINavigationController。

当我不想让子 VC 干扰我明智地选择的状态栏样式时,我在 Swift 中就是这样做的:

override func childViewControllerForStatusBarStyle() -> UIViewController? {
    return nil // ignore childs and let this Navigation Controller handle the StatusBar style
}

override func preferredStatusBarStyle() -> UIStatusBarStyle {
    return .LightContent // or .Default depending on your Style
}

这需要 iOS8 和更新版本,并且仅当您将 Info.plist 中的密钥 UIViewControllerBasedStatusBarAppearance 设置为 YES 时才可用。

奖励:如果这对调用者没有帮助,请在所示的 Ccontroller 中使用它。我检查了我的项目,其中一个项目也在 NavigationController 中,显示为 PopOver 并且从今天开始工作正常。

【讨论】:

  • 这并不能真正回答问题,我的意思是,一般来说,您希望 UIPresentationController 可重用,而不需要在其所有呈现的视图控制器中实现某些东西。并且在呈现视图控制器中实现任何东西而不是呈现不会做任何事情,这就是问题所在......但是,是的,你对“奖金”是正确的,这是目前唯一的方法。
  • @TedBuckland 你是对的,很抱歉这并没有真正回答你的问题,为什么会发生这种情况。老实说:我无法回答这部分问题,只是展示一种我知道如何规避问题的方法。在某些情况下,iOS 的状态栏处理显然是错误的。这应该不是必需的,可能最好打开 Radar Ticket。我有一个关于 UIActivityViewController 的关于这个问题的帖子,目前无法修复:stackoverflow.com/questions/35874582/….
猜你喜欢
  • 2017-11-09
  • 2021-05-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-01
  • 2012-06-20
相关资源
最近更新 更多