ios - 如何将 modalPresentationCapturesStatusBarAppearance = NO 与自定义 UIPresentationController 一起使用?

标签 ios uiviewcontroller uistatusbar uimodalpresentationstyle uipresentationcontroller

我有一个自定义 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 属性吗?

谢谢! :)

最佳答案

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

当我不希望子 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 和更新版本,并且只有在设置 key UIViewControllerBasedStatusBarAppearance 时才可用在你的Info.plistYES .

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

关于ios - 如何将 modalPresentationCapturesStatusBarAppearance = NO 与自定义 UIPresentationController 一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31660920/

相关文章:

iphone - 更改状态栏高度时更改 View 框架?

iphone - 将 uiview 添加到 uiviewcontroller

ios - 启动时状态栏为白色不透明

ios - Theos实例方法调用

iphone - 如何获取新图片,修改图片名称没有效果,还是读取旧图片

ios - 容器 View Controller 中的自定义 topLayoutGuide 长度

iphone - 在父 ViewController 中获取当前 ViewController 名称

iphone - UIStatusBarStyle PreferredStatusBarStyle 在 iOS 7 上不起作用

ios - UIButton 的圆角半径问题

ios - Travis CI 的完整 Xcode 版本和设备名称/操作系统列表?