69

我有一個視圖控制器層次結構和最上面的控制器被顯示爲模式和想知道如何顯示導航欄使用presentViewController和顯示導航欄

'UIViewController:presentViewController:viewControllerToPresent:animated:completion' 

關於「presentViewController該文檔時:注:

'在iPhone和iPod touch上,呈現的視圖始終爲全屏。 在iPad上,演示文稿取決於 modalPresentationStyle屬性中的值。'

對於「modalPresentationStyle」,文檔說:

呈現樣式決定如何一個模態呈現視圖控制器在屏幕上顯示。在iPhone和iPod touch上,模式視圖控制器總是全屏顯示,但在iPad上有幾種不同的顯示選項。

有沒有辦法確保導航欄在狀態欄下方可見,一旦視圖控件顯示出來?我是否應該將文檔解釋爲,您沒有獲得iPhone/iPod的任何選項,只能在iPad上使用?

以前,我使用的是'UIViewController:presentModalViewController:animated',它工作正常,但自iOS 5.0以來,API已被棄用,因此我正在切換到新的。

從視覺上看,我期望做的就是讓新控制器從屏幕底部滑入,就像舊API一樣。

[代碼爲更新]:

// My root level view: 
UIViewController *vc = [[RootViewController alloc] 
          initWithNibName:nil 
          bundle:[NSBundle mainBundle]]; 
navController = [[UINavigationController alloc] initWithRootViewController:vc];   
.... 

// Within the RootViewController, Second view controller is created and added 
// to the hierarchy. It is this view controller that is responsible for 
// displaying the DetailView: 
SecondTierViewController *t2controller = [[SecondTierViewController alloc] 
              initWithNibName:nil 
              bundle:[NSBundle mainBundle]]; 

[self.navigationController pushViewController:t2controller animated:YES]; 

// Created by SecondTierViewController 
DetailViewController *controller = [[DetailViewController alloc] initWithNibName:nil                     
             bundle:[NSBundle mainBundle]]; 

controller.modalTransitionStyle = UIModalTransitionStyleCoverVertical; 
controller.modalPresentationStyle = UIModalPresentationCurrentContext; 

[self.navigationController presentViewController:controller 
             animated:YES 
             completion:nil]; 

回答

163

這是事實,如果你目前在iPhone上一個視圖控制器模態,它總是會不管你如何表現它導航控制器的頂視圖控制器上呈現全屏或任何其他方式。但你總是可以顯示導航欄是這樣的:

而不是呈現該視圖控制器模式地設置爲視圖控制器的根視圖控制器模態呈現導航控制器,你想:

MyViewController *myViewController = [[MyViewController alloc] initWithNibName:nil bundle:nil]; 
UINavigationController *navigationController = 
    [[UINavigationController alloc] initWithRootViewController:myViewController]; 

//now present this navigation controller modally 
[self presentViewController:navigationController 
        animated:YES 
        completion:^{ 

         }]; 

你應該看到一個導航欄,當你的視圖模態地呈現。

+0

這幾乎是我開始的。但是我沒有使用'presentModalViewController'的原因是因爲它被稱爲不贊成使用的API。 – 2012-03-15 18:22:09

+0

這是在UIViewController類中寫入的內容: //將另一個視圖控制器顯示爲模態子項。如果使用動畫,則使用垂直圖紙過渡。此方法已由presentViewController取代:動畫:完成: //將進行相應的計劃。 (void)presentModalViewController:(UIViewController *)modalViewController animated:(BOOL)animated; 所以只需調用新方法並通過零來完成,你應該很好。 – 2012-03-15 18:24:34

+0

很好的回答。更新爲使用'(void)presentViewController:(UIViewController *)viewControllerToPresent animated:(BOOL)flag completion:(void(^)(void))completion' – 2014-07-18 15:00:59

0

如果您沒有設置modalPresentationStyle屬性(如到UIModalPresentationFormSheet),導航欄會一直顯示。爲確保始終如一

[[self.navigationController topViewController] presentViewController:vieController 
                  animated:YES 
                  completion:nil]; 

這將始終顯示導航欄。

+0

嗯..即使對'topViewController'的固定引用,我仍然看到相同的行爲。我不認爲我以任何特殊方式將其他視圖控制器添加到導航堆棧中。 – 2012-03-15 17:55:17

0

一個解決方案

DetailViewController *controller = [[DetailViewController alloc] initWithNibName:nil                     
             bundle:[NSBundle mainBundle]]; 

UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:controller]; 
navController.modalTransitionStyle = UIModalTransitionStyleCoverVertical; 
navController.modalPresentationStyle = UIModalPresentationCurrentContext; 



[self.navigationController presentViewController:navController 
             animated:YES 
             completion:nil]; 
+0

它根本不工作 – 2013-06-14 13:54:16

+0

它解決了我的問題 – Clement 2018-02-13 13:54:17

22

你可以使用:

[self.navigationController pushViewController:controller animated:YES]; 

讓我們回到(我認爲):

[self.navigationController popToRootViewControllerAnimated:YES]; 
+3

謝謝,如果您的故事板中已經有導航控制器設計,那麼最好的辦法就是做到這一點。你幫了我很多 – phyzalis 2014-03-04 01:11:36

+0

回去是[self.navigationController popViewControllerAnimated:YES]; popToRoot - 一路回到第一個視圖控制器 – 2017-02-01 19:08:36

1

所有一[self.navigationController pushViewController:controller animated:YES];確實是動畫過渡,並將其添加到導航控制器堆棧,以及其他一些酷導航欄動畫的東西。如果你不關心酒吧動畫,那麼這個代碼應該工作。該欄確實出現在新的控制器上,並且您可以獲得交互式的流行手勢!

//Make Controller 
DetailViewController *controller = [[DetailViewController alloc] initWithNibName:nil                     
            bundle:[NSBundle mainBundle]]; 
//Customize presentation 
controller.modalTransitionStyle = UIModalTransitionStyleCoverVertical; 
controller.modalPresentationStyle = UIModalPresentationCurrentContext; 

//Present controller 
[self presentViewController:controller 
        animated:YES 
       completion:nil]; 
//Add to navigation Controller 
[self navigationController].viewControllers = [[self navigationController].viewControllers arrayByAddingObject:controller]; 
//You can't just [[self navigationController].viewControllers addObject:controller] because viewControllers are for some reason not a mutable array. 

編輯:對不起,presentViewController將填滿整個屏幕。您需要進行自定義轉換,使用CGAffineTransform.translation或其他方法,使用轉換對控制器進行動畫處理,然後將其添加到navigationController的viewControllers。

1

我在ios7上遇到了同樣的問題。我在選擇器中調用它,它在ios7和ios8上都可以工作。

[self performSelector: @selector(showMainView) withObject: nil afterDelay: 0.0]; 

- (void) showMainView { 
    HomeViewController * homeview = [ 
     [HomeViewController alloc] initWithNibName: @ 
     "HomeViewController" 
     bundle: nil]; 
    UINavigationController * navcont = [ 
     [UINavigationController alloc] initWithRootViewController: homeview]; 
    navcont.navigationBar.tintColor = [UIColor whiteColor]; 
    navcont.navigationBar.barTintColor = App_Theme_Color; 
    [navcont.navigationBar 
    setTitleTextAttributes: @ { 
     NSForegroundColorAttributeName: [UIColor whiteColor] 
    }]; 
    navcont.modalPresentationStyle = UIModalPresentationFullScreen; 
    navcont.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal; 
    [self.navigationController presentViewController: navcont animated: YES completion:^{ 

    }]; 
} 
0

我使用此代碼。它的正常工作中的iOS 8

MyProfileEditViewController *myprofileEdit=[self.storyboard instantiateViewControllerWithIdentifier:@"myprofileeditSid"]; 
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:myprofileEdit]; 
[self presentViewController:navigationController animated:YES completion:^{}]; 
20

雨燕3.0/4.0

導航:

guard let myVC = self.storyboard?.instantiateViewController(withIdentifier: "MyViewController") else { return } 
let navController = UINavigationController(rootViewController: myVC) 

self.navigationController?.present(navController, animated: true, completion: nil) 

回去:

self.dismiss(animated: true, completion: nil) 

夫特2.0

導航:

let myVC = self.storyboard?.instantiateViewControllerWithIdentifier("MyViewController"); 
let navController = UINavigationController(rootViewController: myVC!) 

self.navigationController?.presentViewController(navController, animated: true, completion: nil) 

回去:

self.dismissViewControllerAnimated(true, completion: nil) 
+0

但如何設置當我嘗試你的代碼,然後只設置導航欄,但我不能改變它的屬性像棒色彩,標題等 – Jaydip 2017-10-09 05:59:39

+0

你是什麼問題@Jaydip? – 2017-10-09 06:01:17

+0

navigationBartintColor不變 – Jaydip 2017-10-09 07:26:04

0

如果您在斯威夫特2.x中使用NavigationController

let storyboard = UIStoryboard(name: "Main", bundle: nil) 
let targetViewController = storyboard.instantiateViewControllerWithIdentifier("targetViewControllerID") as? TargetViewController 
self.navigationController?.pushViewController(targetViewController!, animated: true) 
0

試試這個

 let transition: CATransition = CATransition() 
    let timeFunc : CAMediaTimingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut) 
    transition.duration = 1 
    transition.timingFunction = timeFunc 
    transition.type = kCATransitionPush 
    transition.subtype = kCATransitionFromRight 
    self.view.window!.layer.addAnimation(transition, forKey: kCATransition) 
    self.presentViewController(vc, animated:true, completion:nil) 
0

斯威夫特版本: 這提出並嵌入導航控制器,一個視圖控制器。

override func viewDidAppear(animated: Bool) { 
    super.viewDidAppear(animated) 

    // Identify the bundle by means of a class in that bundle. 
    let storyboard = UIStoryboard(name: "Storyboard", bundle: NSBundle(forClass: SettingsViewController.self)) 

    // Instance of ViewController that is in the storyboard. 
    let settingViewController = storyboard.instantiateViewControllerWithIdentifier("SettingsVC") 

    let navController = UINavigationController(rootViewController: settingViewController) 

    presentViewController(navController, animated: true, completion: nil) 

} 
1

夫特3

 let vc0 : ViewController1 = ViewController1() 
     let vc2: NavigationController1 = NavigationController1(rootViewController: vc0) 
     self.present(vc2, animated: true, completion: nil) 
+0

如何將BACK按鈕添加到呈現的UIViewController – 2017-05-09 09:55:06

+0

@zulkarnainshah那是什麼? – BennyTheNerd 2017-05-11 16:47:32