Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Top Bar does not appear for presentModalViewController

I've created a UIViewController subclass called addItemToListViewController. I selected add an "xib" as well, and just created a simple page with a couple of labels and a textField. In the interface builder I selected "Top Bar - Navigation Bar" so that when it is put on the stack when the application runs it will have a top bar that will match the initial main window. In the Interface builder it shows the top border, but when I run the application in the simulator the top bar is not present once the view is displayed.

Here is the code I placed in the rootViewController to present the view controller

- (IBAction)addButtonPressed:(id)sender 
{   
    AddItemToListViewController *addItemToListViewController = [[AddItemToListViewController alloc] initWithNibName: @"AddItemToListViewController" bundle:nil];

    [self presentModalViewController: AddItemToListViewController animated: YES];
    [AddItemToListViewController release];
}

I'm only able to have the top bar present if I manually add a Navigation bar to the xib. If I must add a Navigation bar to my xib, what is the purpose of the "Top Bar" attribute?

like image 470
5StringRyan Avatar asked Dec 28 '22 14:12

5StringRyan


2 Answers

- (IBAction)addButtonPressed:(id)sender 
{   
    AddItemToListViewController *addItemToListViewController = [[AddItemToListViewController alloc] initWithNibName: @"AddItemToListViewController" bundle:nil];
    UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:addItemToListViewController];
    [self presentModalViewController: navController animated: YES];
    [AddItemToListViewController release];
    [navController release];
}
like image 178
Robin Avatar answered Dec 30 '22 03:12

Robin


That "top bar - Navigation bar" in InterfaceBuilder is what's known as a "Simulated Metric". It's there to help you lay out your view with correct spacing when other visual elements - the status bar, navigation bar, or tab bar - might consume some of the device's screen real estate. It doesn't actually do anything other than shrink the vertical dimensions of the view defined by the NIB. The purpose is to help you layout your view, not to actually create a component that will appear in your app.

If you want a navigation bar, then you have two choices. The first choice is to use a navigation controller (of which your initial view will have to be the root) and call

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

The process of setting up a navigation controller correctly, etc, is nontrivial, and you should do some searching to find the best way to do that for your app. For a simple app, especially if you're just learning iOS, you can use the "Navigation-based Application" template when you create a new project. With a navcon, you get all the fancy behavior normally associated with that top bar - an automatic back button, fancy left/right scrolling when you transition to a detail view, etc.

The second option is to put a "fake" navigation bar in the detail view, using the Navigation Bar object. You can find that object, plus some other related objects, in the bottom half of the "Utilities View" (the right-most pane) in XCode. Just drag the object into your XIB and blammo, you have a 44-pixel tall gray bar. This navigation bar is just like what you get when you use a Navigation Controller except you don't get the stack functionality; you can still add buttons to the left and right, change the title, tint it to a specific color, etc.

like image 45
AndrewS Avatar answered Dec 30 '22 05:12

AndrewS