如何将状态栏样式更新为自定义过渡的一部分

时间:2014-04-27 21:52:02

标签: ios iphone objective-c ios7

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

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

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

这是我的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];
                     }];
}

任何指针都非常赞赏!

2 个答案:

答案 0 :(得分:29)

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

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

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

有一些more information here。希望有所帮助!

答案 1 :(得分:0)

这对我有用:

    [UIView animateWithDuration:0.25
                          delay:0
                        options:UIViewAnimationOptionCurveEaseOut
                     animations:^{
                         _preferredBarStyle = UIStatusBarStyleLightContent;
                         [self setNeedsStatusBarAppearanceUpdate];
                     }];

然后你只需要在preferredStatusBarStyle方法上返回这个值:

- (UIStatusBarStyle) preferredStatusBarStyle {
    return _preferredBarStyle;
}

我希望它有所帮助!