首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在自定义UIPresentationController中使用modalPresentationCapturesStatusBarAppearance = NO?

如何在自定义UIPresentationController中使用modalPresentationCapturesStatusBarAppearance = NO?
EN

Stack Overflow用户
提问于 2015-07-28 02:47:21
回答 1查看 3.7K关注 0票数 4

我有一个定制的UIPresentationController,并覆盖了定制viewController表示的frameOfPresentedViewInContainerView。除了状态栏之外,一切都很正常。

我不希望状态栏改变外观,- it应该停留在它以前的样子。现在苹果的文档:https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIViewController_Class/#//apple_ref/occ/instp/UIViewController/modalPresentationCapturesStatusBarAppearance说如果modalPresentationStyle不是UIModalPresentationFullScreen或者modalPresentationCapturesStatusBarAppearance是NO,我应该是好的,状态栏不应该改变。

使用以下代码:

代码语言:javascript
复制
- (BOOL)prefersStatusBarHidden {
    NSLog(
        @"prefersStatusBarHidden was called %d %ld",
        self.modalPresentationCapturesStatusBarAppearance,
        (long)self.modalPresentationStyle
    );

    return YES;
}

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

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

但是为什么呢?

我的想法是,iOS认为viewController是全屏显示的,尽管事实并非如此。

我发现了UIPresentationController的属性shouldPresentInFullscreen -它默认返回YES。返回NO没有任何帮助,所以我甚至不明白这个属性是做什么的……从字面上看它没有效果。这同样适用于presentationStyle属性-我看不到更改它会有任何影响。我本以为presentationStyle属性会被“重定向”到viewControllers modalPresentationStyle属性,但它保留在UIModalPresentationCustom中,它必须在一开始就启动自定义表示。

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

谢谢!:)

EN

回答 1

Stack Overflow用户

发布于 2016-03-04 08:21:15

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

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

代码语言:javascript
复制
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时才可用。

额外好处:如果这对调用者没有帮助,可以在所示的C控制器中使用它。我检查了我的项目,在其中一个项目中,它也在NavigationController中显示为PopOver,并且到今天为止工作正常。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31660920

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档