2014-04-29 30 views
8

我正在改變根視圖控制器,通過按下一個連接到我想要改變到的視圖控制器的自定義繼續按鈕。自定義賽格看起來像:iOS - 用動畫改變根視圖控制器

- (void)perform{ 
    UIViewController *source = (UIViewController *)self.sourceViewController; 
    source.view.window.rootViewController = self.destinationViewController; 
} 

但這只是立即改變根視圖控制器。我希望控制器在舊控制器上淡出。

+0

可能重複http://stackoverflow.com/questions/10961926/how-do-i-do -a-fade-no-transition-between-view-controllers) – BangOperator

+0

你的代碼隱藏了導航條 – user2070775

回答

3

我覺得有很多答案這個了,但這樣的事情應該工作:

[UIView transitionWithView:self.sourceViewController.view.window 
        duration:0.5 
        options:UIViewAnimationOptionTransitionCrossDissolve 
       animations:^{ 
        self.sourceViewController.view.window.rootViewController = self.destinationViewController; 
       } completion:^(BOOL finished) { 
        // Code to run after animation 
       }]; 
20

常見的方式做到這一點是:

UIStoryboard *sb = [UIStoryboard storyboardWithName:@"YourSBName" bundle:nil]; 
    UIViewController *vc = [sb instantiateInitialViewController];// Or any VC with Id 
    DictateITAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate]; 
    appDelegate.window.rootViewController = vc; // PLEASE READ NOTE ABOUT THIS LINE 
    [UIView transitionWithView:appDelegate.window 
         duration:INTERVAL_DURATION 
         options:UIViewAnimationOptionTransitionCrossDissolve 
        animations:^{ appDelegate.window.rootViewController = vc; } 
        completion:nil]; 

現在的幾個注意事項:

appDelegate.window.rootViewController = vc; // PLEASE READ NOTE ABOUT THIS LINE 

您實例化的新視圖控制器將以默認方向呈現,即縱向。所以可能如果你在風景中做到這一點,你的新視圖控制器將顯示爲肖像,然後轉向風景。這條線爲我解決了這個問題。

2

我需要翻譯成雨燕3.0爲我的當前項目。這裏有一個辦法做到這一點:

//Get the storyboard that contains the VC you want 
let storyboard = UIStoryboard(name: "Main", bundle: nil) 

//Get the VC you want to push onto the stack 
//You can use storyboard.instantiateViewController(withIdentifier: "yourStoryboardId") 
guard let vc = storyboard.instantiateInitialViewController() else { return } 

//Get the current app delegate 
guard let appDel = UIApplication.shared.delegate as? AppDelegate else { return } 

//Set the current root controller and add the animation with a 
//UIView transition 
appDel.window?.rootViewController = vc 

UIView.transition(with: appDel.window!, 
       duration: 0.25, 
       options: .transitionCrossDissolve, 
      animations: { appDel.window?.rootViewController = vc } , 
      completion: nil) 
[我如何做一個淡入/否視圖控制器之間的轉換(的
相關問題