2016-06-11 33 views
0

我在我的應用程序中有一個UInaviguationController。我的問題是如何在將viewController推送到當前場景的視圖時製作backgroundView。默認是黑屏。我希望實現類似於在UINaviguationController中呈現Modally而不是當前上下文的東西。當推入UI導航時更改背景控制器

回答

0

比方說,你是從視圖控制器A到視圖控制器B.

你可以嘗試像服用右背景視圖的屏幕截圖您在使用導航控制器推B之前。

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]; 
    }]; 
} 
+0

謝謝,你可以爲你解答迅速的版本?另外如何通過拍攝的圖像? – user2232305

+0

在目標C中的頭文件中設置一個強大的UIImage屬性。當您編寫推視圖控制器B的代碼時,您必須編寫 ViewControllerB.image = yourImage; 對不起,我不是很熟悉迅速,並可能最終誤導你。我已經告訴過你如何做到客觀C,你應該能夠迅速做出想法並做到這一點。 – Rikh