如何作为自定义转换的一部分来更新StatusBar样式

我正在使用iOS 7的UIviewControllerAnimatedTransitioning协议提供一个自定义animation模式的ViewController 。 animation工作正常,但是,我想新呈现的ViewController具有不同于呈现VC的状态栏风格。

我所看到的是-(UIStatusBarStyle)preferredStatusBarStyle在PRESENTING ViewController上被调用(事实上有好几次),从来没有在新呈现的ViewController上调用。 如果我删除自定义animation与状态栏一切工作正如我所料。

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

任何指针非常感谢!

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

 UIViewController *controllerToPresent = [...] controllerToPresent.modalPresentationStyle = UIModalPresentationStyleCustom; controllerToPresent.modalPresentationCapturesStatusBarAppearance = YES; [self presentViewController:controllerToPresent animated:YES completion:nil]; 

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

这对我工作:

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

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

 - (UIStatusBarStyle) preferredStatusBarStyle { return _preferredBarStyle; } 

我希望它有帮助!