简体   繁体   中英

How to push UIViewController with replaced UINavigationController

I have a custom UINavigationController that draws a custom background. That navigation controller is already set when accessing self.navigationController on the root view controller. Now what I'd like to do is to push a new view controller but replace the navigation controller in the new view controller to a default one (without the custom background).

Basically the new view controller is presenting an image with the navigation bar being translucent. Because it reuses the navigation controller (with the custom background) from the root view controller, the end effect is translucent navigation bar with custom background.

I know I could replace the navigation bar when presenting the new view controller as a modal view. But can I replace it when pushing the new view controller?

I tried something like this but this code has no effect:

LTImageViewController* controller = [[LTImageViewController alloc] init];
UINavigationController* navController = [[UINavigationController alloc] initWithRootViewController:self];
[navController pushViewController:controller animated:YES];

You can't just push a view controller onto any old navigation controller's stack. (Well, you can, but you won't get very far.) The nav controller should be the root view controller for the app's window. If you want to use your custom navigation controller, you should replace the existing navigation controller with your custom version. You'd do something like:

AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
delegate.window.rootViewController = [[MyCustomNavController alloc] init];

While pushing a view controller in UINavigationController it creates an stack, and i think we can not push an navigation controller in another.

You can do one thing show your viewController modally and change the TransitionStyle to UIModalTransitionStyleCoverVertical

Might Help

If your problem is just the style/color/image of your navigationBar, in the viewWillAppear: of your viewControllers you can just set:

 [self.navigationController.navigationBar setBackgroundImage:yourImage                             
                                               forBarMetrics:UIBarMetricsDefault];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM