UIViewController 的 prefersStatusBarHidden 不起作用 [英] UIViewController's prefersStatusBarHidden not working

查看:101
本文介绍了UIViewController 的 prefersStatusBarHidden 不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试隐藏我的一个视图控制器的状态栏(以模态显示时).当我呈现视图控制器时,状态栏是隐藏的,然后在关闭时返回.

I am trying to make the status bar for one of my view controllers to be hidden (when displayed modally). When I'm presenting the view controller, the status bar is is to be hidden and then returned when dismissed.

我已将以下代码添加到呈现的视图控制器

I have added the following code to the presented view controller

- (BOOL)prefersStatusBarHidden
{
    return YES;
}

我还将 Info.plist 文件中的键设置如下:

I have also set the keys in the Info.plist file to the following:

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

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

From my understanding this should be all that is required to make this work.

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

I am also using a custom Animation Controller to do the presenting which conforms to the UIViewControllerAnimatedTransitioning protocol. In the animateTransition: implementation I have tried to manually call prefersStatusBarHidden, followed by setNeedsStatusBarAppearanceUpdate to ensure the call is being made, but the status bar remains.

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

Any ideas why this is happening would be appreciated. I have searched StackOverflow, but it appears no one has had this issue, all accepted answers refer to calling setNeedsStatusBarAppearanceUpdate, which I am already doing.

EDIT - 下面的代码现在似乎可以WORK如愿

EDIT - The code below now seems to WORK as desired

- (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 参考.

In iOS7, there's actually a new property for UIViewController called modalPresentationCapturesStatusBarAppearance. Apple iOS reference.

默认值为 NO.

当您通过调用 presentViewController:animated:completion: 方法呈现视图控制器时,只有当呈现的控制器的 modalPresentationStyle 值为 UIModalPresentationFullScreen 时,状态栏外观控制才会从呈现转移到呈现的视图控制器.通过将此属性设置为 YES,您可以指定呈现的视图控制器控件状态栏外观,即使呈现非全屏.

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),如果要捕获状态栏,则必须设置此.要使用,您所要做的就是在呈现的视图控制器上将其设置为 YES:

Therefore, for any presentationStyle other than the normal fullscreen (for example: UIModalPresentationCustom), this must be set if you want to capture the status bar. To use, all you have to do is set it to YES on the view controller that's being presented:

toVC.modalPresentationCapturesStatusBarAppearance = YES;

这篇关于UIViewController 的 prefersStatusBarHidden 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆