推入UI导航控制器时更改背景

时间:2016-06-11 15:27:53

标签: ios arrays uinavigationcontroller

我的App中有一个UInaviguationController。我的问题是如何在将viewController推送到当前场景的视图时制作backgroundView。默认为黑屏。我想实现类似于在UINaviguationController中的当前Context上呈现Modally的东西。

1 个答案:

答案 0 :(得分:0)

假设您从View Controller A转到View Controller B.

在使用导航控制器推送B之前,您可以尝试使用A的背景视图的屏幕截图。

UIGraphicsBeginImageContextWithOptions(targetView.bounds.size, NO, 0);
[targetView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

然后将此图像传递给视图控制器B并将其设置为背景图像。如果您还想要以与模式方式呈现视图控制器相同的动画,则必须使用自定义导航控制器动画并进行设置。

编辑:如果您想要做的就是在B上显示与A相同的纯色。只需将B的背景颜色设置为A的背景颜色并添加自定义导航控制器动画

对于动画, 您必须将View Controller设置为navigationController的委托。然后,您将必须添加以下委托函数:

-(id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController animationControllerForOperation:(UINavigationControllerOperation)operation fromViewController:(UIViewController *)fromVC toViewController:(UIViewController *)toVC
        {
        return [[Slide alloc] init];
    }

然后返回您将创建的类以实现动画。您必须使您所制作的课程符合<UIViewControllerAnimatedTransitioning>协议

该类必须实现以下两个委托函数 -

- (NSTimeInterval)transitionDuration:(id <UIViewControllerContextTransitioning>)transitionContext{
        return 0.4f;
}

- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext{
UIViewController* toController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
UIViewController* fromController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
UIView* container = [transitionContext containerView];
[container addSubview:toController.view];

 [UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
        //set frame to be out of screen bounds here
    } completion:^(BOOL finished) {
        [fromController.view removeFromSuperview];
        [transitionContext completeTransition:finished];
    }];
}