So I’m building an app and I am running through a few ViewControllers that don’t need to know about each other, so I start off switching through views like so…
// remove the previous view in order to load in the new view
NSArray *sViews = [self.view subviews];
[sViews makeObjectsPerformSelector:@selector(removeFromSuperview)];
// create the new view, in this case the user wishes to
BaseViewController *baseVC = [[BaseViewController alloc] initWithNibName:@"BaseViewController" bundle:[NSBundle mainBundle]];
self.baseViewController = baseVC;
[baseVC release];
// add the newly created view to the screen
[self.view insertSubview:baseViewController.view atIndex:0];
The above is the view controller that I want the navigation controller to reside in. So within the .m of this view controller I created a UINavigationController as a member variable and named it navController. I then tried implementing a UINavigationController using the code below.
UIViewController *control = [[BusinessDisplayViewController alloc] initWithNibName:@"BusinessDisplayViewController" bundle: nil];
navController = [[UINavigationController alloc] initWithRootViewController:control];
[self presentModalViewController:navController animated:YES];
The problem I’m running into is two fold. First, when the BusinessDisplayViewController (below) is loaded there is a 20 pixel or so gap between my mapView and tableView that isn’t there when I was loading it using insertSubview: not sure why that would be. Second, once I’m in BusinessDisplayViewController.m I’m not sure how to access the navigationController created in BaseViewController. Could someone explain why my view would be effected, how I could access the navigationController or if I’m even going about this the right way.
UINavigationControlleris designed for use in one of three possible contexts on iPhone:UITabBarController.presentModalViewController:animated:.In your case, the
UINavigationControllerhas configured itself for presentation as a subview of the window. This is why you see the 20 pixel gap at the top. Since the window object underlaps the status bar,UINavigationControlleroffsets the position of its navigation bar by 20 pixels (or more, if you’re on a phone call).The standard way to use
UINavigationControlleras your root view controller is to construct it as a property of your app delegate inapplication:didFinishLaunchingWithOptions:, and add its view as a subview of the window. Then within any view controller you push onto your navigation stack, you can access the navigation controller object usingself.navigationController.