如何将StatusBar样式更新为自定义转换的一部分 [英] How do I update the StatusBar Style as part of a custom transition

查看:132
本文介绍了如何将StatusBar样式更新为自定义转换的一部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用iOS 7 UIviewControllerAnimatedTransitioning 协议来呈现带有自定义动画的模态 ViewController 。动画工作正常,但是,我希望新呈现的 ViewController 具有与呈现VC不同的状态栏样式。

I'm using the iOS 7 UIviewControllerAnimatedTransitioning protocol to present a modal ViewController with a custom animation. The animation works correctly, however, I want the newly presented ViewController to have a different status bar style than the presenting VC.

我所看到的是 - (UIStatusBarStyle)preferredStatusBarStyle 在PRESENTING ViewController 上被调用(好几次)实际上)并且永远不会出现在新呈现的 ViewController 上。如果我删除自定义动画,状态栏的所有内容都按照我的预期运行。

What I'm seeing is that -(UIStatusBarStyle)preferredStatusBarStyle gets called on the PRESENTING ViewController (several times in fact) and never on the newly presented ViewController. If I remove the custom animation everything with the status bar works as I'd expect.

我的animateTransition函数中是否需要做一些特殊操作来更新根视图控制器什么的?我尝试用 [UIApplication sharedApplication] setStatusBarStyle 手动设置statusBar但它不起作用(我想因为我使用的是基于ios 7视图控制器的状态栏样式)。

Is there something special I need to do in my animateTransition function to update the root view controller or something? I've tried manually setting the statusBar with [UIApplication sharedApplication] setStatusBarStyle but it doesn't work (I think because I'm using the ios 7 view controller based status bar styling).

这是我的animateTransition代码:

This is my code for animateTransition:

- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext
{
    UICollectionViewCell *activeCell;
    if ([self.collectionView.visibleCells containsObject:self.cellForActiveIdeaVC]) {
        activeCell = self.cellForActiveIdeaVC;
    }

    UIView *container = transitionContext.containerView;
    UIViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    UIViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    UIView *fromView = fromVC.view;
    UIView *toView = toVC.view;

    CGRect beginFrame;
    if (activeCell) {
        beginFrame = [container convertRect:activeCell.bounds fromView:activeCell];
    } else {
        beginFrame = CGRectMake(container.width / 2, container.height / 2, 0, 0);
    }

    CGRect endFrame = [transitionContext initialFrameForViewController:fromVC];

    UIView *move = nil;
    if (toVC.isBeingPresented) {
        toView.frame = endFrame;
        move = [toView snapshotViewAfterScreenUpdates:YES];
        move.frame = beginFrame;
    } else {
        if (activeCell) {
            move = [activeCell snapshotViewAfterScreenUpdates:YES];
        } else {
            move = [fromView snapshotViewAfterScreenUpdates:YES];
        }
        move.frame = fromView.frame;
        [fromView removeFromSuperview];
    }
    [container addSubview:move];

    [UIView animateWithDuration:.5
                          delay:0
         usingSpringWithDamping:700
          initialSpringVelocity:15
                        options:0
                     animations:^{
                         move.frame = toVC.isBeingPresented ?  endFrame : beginFrame;
                     }
                     completion:^(BOOL finished) {
                         [move removeFromSuperview];

                         if (toVC.isBeingPresented) {
                             toView.frame = endFrame;
                             [container addSubview:toView];
                         } else {
                             if (self.cellForActiveIdeaVC) {
                                 self.cellForActiveIdeaVC = nil;
                             }
                         }

                         [transitionContext completeTransition:YES];
                     }];
}

任何指针都非常赞赏!

推荐答案

使用iOS 7自定义转换,可以呈现非全屏视图控制器,因此不会影响状态栏外观。您必须明确告诉iOS您的自定义显示的视图控制器实际上将控制状态栏的外观。

With iOS 7 custom transitions, it's possible to present a view controller that isn't fullscreen and therefore wouldn't affect the statusbar appearance. You have to explicitly tell iOS that your custom presented view controller will, in fact, control the status bar's appearance.

UIViewController *controllerToPresent = [...]
controllerToPresent.modalPresentationStyle = UIModalPresentationStyleCustom;
controllerToPresent.modalPresentationCapturesStatusBarAppearance = YES;

[self presentViewController:controllerToPresent animated:YES completion:nil];

有一些这里有更多信息。希望有所帮助!

There's some more information here. Hope that helps!

这篇关于如何将StatusBar样式更新为自定义转换的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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