UIViewController更喜欢StatusBarHidden无法正常工作 [英] UIViewController's prefersStatusBarHidden not working

查看:596
本文介绍了UIViewController更喜欢StatusBarHidden无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试隐藏其中一个视图控制器的状态栏(以模态方式显示)。当我呈现视图控制器时,状态栏将被隐藏,然后在被解雇时返回。

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 - 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。

Default value is 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更喜欢StatusBarHidden无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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