ios - UIViewController 的 prefersStatusBarHidden 不起作用

标签 ios objective-c ios7 uiviewcontroller ios7-statusbar

我正在尝试隐藏我的一个 View Controller 的状态栏(当模态显示时)。当我展示 View Controller 时,状态栏将被隐藏,然后在关闭时返回。

我已将以下代码添加到呈现的 View Controller 中

- (BOOL)prefersStatusBarHidden
{
    return YES;
}

我还将 Info.plist 文件中的键设置为以下内容:

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

据我了解,这应该是完成这项工作所需的全部内容。

我还使用自定义动画 Controller 进行呈现,它符合 UIViewControllerAnimatedTransitioning 协议(protocol)。在 animateTransition: 实现中,我尝试手动调用 prefersStatusBarHidden,然后是 setNeedsStatusBarAppearanceUpdate 以确保调用正在进行,但状态栏仍然存在.

任何想法为什么会发生这种情况将不胜感激。我已经搜索过 StackOverflow,但似乎没有人遇到过这个问题,所有接受的答案都是指调用 setNeedsStatusBarAppearanceUpdate,我已经在这样做了。

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

- (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 实际上有一个新属性,称为 modalPresentationCapturesStatusBarAppearanceApple iOS reference.

Default value is NO.

When you present a view controller by calling the presentViewController:animated:completion: method, status bar appearance control is transferred from the presenting to the presented view controller only if the presented controller’s modalPresentationStyle value is UIModalPresentationFullScreen. By setting this property to YES, you specify the presented view controller controls status bar appearance, even though presented non–fullscreen.

The system ignores this property’s value for a view controller presented fullscreen.

因此,对于普通全屏以外的任何presentationStyle(例如:UIModalPresentationCustom),如果你想捕获状态栏,这个必须设置。要使用,您只需在正在呈现的 View Controller 上将其设置为 YES:

toVC.modalPresentationCapturesStatusBarAppearance = YES;

关于ios - UIViewController 的 prefersStatusBarHidden 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23615647/

相关文章:

ios - 如何将 Collection View 设置为第 n 个元素

objective-c - 选择器上的 LLDB 中断不再适用于非调试符号

ios - CGAffineTransformMakeScale animation on a cornerRadius rounded UIButton

ios - Core Data 使用派生类上的谓词获取基类的数据

uiviewcontroller - 将 UIApplication statusBarHidden 设置为 YES 在 iOS 7 中不起作用

ios - 导入 LocalAuthentification.framework 在 iOS 7.1 上崩溃

ios - 以编程方式更新 Xcode Assets 目录

ios - 导入 Swift 与导入基础

ios - iOS 中的中文文本转语音

objective-c - 访问 NSImageView 并给予 EXC_BAD_ACCESS