控制器和显示导航条

我有一个视图控制器层次结构和最顶端的控制器显示为一个模态,并希望知道如何显示导航栏时使用

'UIViewController:presentViewController:viewControllerToPresent:animated:completion'

‘ presViewController: Animated: complete:’注意:

在 iPhone 和 iPod touch 上,呈现的视图总是全屏的。 在 iPad 上,演示内容取决于 ModalPresentationStyle 属性

对于“ modalPresentationStyle”,医生说:

表示样式决定如何在屏幕上显示模态呈现的视图控制器。在 iPhone 和 iPod touch 上,模态视图控制器总是全屏显示,但在 iPad 上有几种不同的显示选项。

一旦视图控件显示自己,是否有办法确保导航栏在状态栏下方可见?我是不是应该把这个文档理解为,你没有任何 iPhone/iPod 的选项,而且只能在 iPad 上使用?

以前,我使用的是 'UIViewController:presentModalViewController:animated',它工作得很好,但是自从 iOS 5.0以来,这个 API 已经被废弃了,所以我要切换到新的 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];
133796 次浏览

If you didn't set the modalPresentationStyle property (like to UIModalPresentationFormSheet), the navigation bar will be displayed always. To ensure, always do

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

This will show the navigation bar always.

One solution

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];

It is true that if you present a view controller modally on the iPhone, it will always be presented full screen no matter how you present it on the top view controller of a navigation controller or any other way around. But you can always show the navigation bar with the following workaround way:

Rather than presenting that view controller modally present a navigation controller modally with its root view controller set as the view controller you want:

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:^{


}];

You should see a navigation bar when your view is presented modally.

Can you use:

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

Going back (I think):

[self.navigationController popToRootViewControllerAnimated:YES];

All a [self.navigationController pushViewController:controller animated:YES]; does is animate a transition, and add it to the navigation controller stack, and some other cool navigation bar animation stuffs. If you don't care about the bar animation, then this code should work. The bar does appear on the new controller, and you get an interactive pop gesture!

//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.

Edit: Sorry, presentViewController will fill the full screen. You will need to make a custom transition, with CGAffineTransform.translation or something, animate the controller with the transition, then add it to the navigationController's viewControllers.

I had the same problem on ios7. I called it in selector and it worked on both ios7 and 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: ^ {


}];
}

I use this code. It's working fine in iOS 8.

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

Swift 5.*

Navigation:

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


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

Going Back:

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

Swift 2.0

Navigation:

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


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

Going Back:

self.dismissViewControllerAnimated(true, completion: nil)

If you use NavigationController in Swift 2.x

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

try this

     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)

Swift version : This presents a ViewController which is embedded in a Navigation Controller.

    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)


}

Swift 3

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