我正在try 隐藏其中一个视图控制器的状态栏(以模式显示时).当我展示视图控制器时,状态栏将被隐藏,然后在关闭时返回.

我已经向显示的视图控制器添加了以下代码

- (BOOL)prefersStatusBarHidden
{
    return YES;
}

我还设置了信息中的键.plist文件到以下位置:

<key>UIViewControllerBasedStatusBarAppearance</key>
<true/>

据我所知,这应该是所有需要使这项工作.

我还使用一个自定义的动画控制器来做演示,这符合UIViewControllerAnimatedTransitioning协议.在animateTransition:实现中,我try 手动调用prefersStatusBarHidden,然后是setNeedsStatusBarAppearanceUpdate,以确保调用正在进行,但状态栏仍然存在.

如果您有任何关于为什么会发生这种情况的 idea ,我们将不胜感激.我已经搜索了StackOverflow,但似乎没有人遇到过这个问题,所有被接受的答案都指向拨打setNeedsStatusBarAppearanceUpdate,我已经在这么做了.

EDIT-下面的代码现在似乎符合要求

- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext
{
    if (self.isPresenting) {
        UIView *containerView = [transitionContext containerView];

        UIViewController *fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
        UIViewController *toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];

        toViewController.view.frame = containerView.frame;

        [containerView addSubview:toViewController.view];

        // Ask the presented controller whether to display the status bar
        [toViewController setNeedsStatusBarAppearanceUpdate];

        [UIView animateWithDuration:1.0f delay:0.0f options:UIViewAnimationOptionCurveEaseIn animations:^{
            toViewController.view.alpha = 1.0f;
            fromViewController.view.alpha = 0.0f;
        } completion:^(BOOL finished) {
            [transitionContext completeTransition:YES];
        }];
    }
    else {
        // do the reverse
        UIViewController *fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
        UIViewController *toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];

        [UIView animateWithDuration:1.0f delay:0.0f options:UIViewAnimationOptionCurveEaseIn animations:^{
            toViewController.view.alpha = 1.0f;
            fromViewController.view.alpha = 0.0f;
        } completion:^(BOOL finished) {
            [transitionContext completeTransition:YES];
            // Once dismissed - ask the presenting controller if the status bar should be presented
            [toViewController setNeedsStatusBarAppearanceUpdate];
        }];
    }
}

....

// PresentingController.m
- (BOOL)prefersStatusBarHidden
{
    if (self.presentedViewController) {
        return YES;
    }
    return NO;
}

// PresentedController.m
- (BOOL)prefersStatusBarHidden
{
    return YES;
}

推荐答案

在iOS7中,UIViewController实际上有一个名为modalPresentationCapturesStatusBarAppearance的新属性.Apple iOS reference.

默认值为否.

通过调用presentViewController:animated:completion:方法呈现视图控制器时,仅当呈现的控制器的modalPresentationStyle值为UIModalPresentationFullScreen时,状态栏外观控制才会从呈现转移到呈现的视图控制器.通过将此属性设置为"是",可以指定显示的视图控制器控制状态栏的外观,即使它不是全屏显示的.

对于全屏显示的视图控制器,系统将忽略此属性的值.

因此,对于正常全屏之外的任何presentationStyle(例如:UIModalPresentationCustom),如果您想捕获状态栏,请点击must be set.要使用,只需在显示的视图控制器上将其设置为YES:

toVC.modalPresentationCapturesStatusBarAppearance = YES;

Objective-c相关问答推荐

如何在情节提要中为视图控制器提供标识符?

错误无法使 UICollectionElementKindCell 类型的视图出列

将 NSArray 保存到 NSUserDefaults 并在 NSMutableArray 中获取它

使用 AVAudioplayer 在后台播放音乐

UIButton 标题更改为默认值

如何删除警告按钮的框架在运行时会有所不同.

Xcode 中架构的重复符号

我应该打电话给 [super awakeFromNib] 吗?

目标 C:在不离开应用程序的情况下发送Electron邮件

Objective C 代码是否有 github markdown 语言标识符?

如何处理 Xcode 警告没有以前的函数原型......?

在Objective C中迭代枚举?

在 ARC 中清零弱引用

如何将当前方法的名称或签名放入 NSString?

iOS 上 64 位的 BOOL

Xcode 7 UI 测试:如何在代码中消除一系列系统alert

@class 在 Objective-C 中做了什么?

Objective-c 协议前向声明

可以在 NSLocalizedString 中使用变量和/或参数吗?

如何为 iPhone 6/7 自定义边到边图像指定尺寸?