简体   繁体   中英

UINavigationBar doesn't appear

I can't understand why Navigation Bar doesn't appear.

For example, I tried to add Navigation Bar like this:

1) File -> new project -> single view application -> storyboard -> viewcontroller -> attributes inspector -> Top Bar -> Navigation Bar. 

It appear at storyboard, but doesn't appear when app run on simulator

Also I can't understand why it doesn't appear as a subview In viewDidLoad :

UINavigationBar *navBar = [[UINavigationBar alloc] init];

[self.view addSubview:navBar]; 

That's not how you add a navigation bar to a view in Storyboard.

The normal way to do this, is to select your View Controller in your story board, then choose "Editor" in your menu, then "Embed in" => "Navigation Controller".

You should see a navigation controller appears with an arrow connecting to your original view controller.

Then, choose your newly appeared navigation controller, check the "Is Initial View Controller" checkbox in the inspector, and viola, that should give you what you need.

And you don't customise your navigation bar though UINavigationBar . Instead, you customise what appears in your navigation bar by changing your view controller's navigationItem .

In your view controller's viewDidLoad: , try the following:

self.navigationItem.title = @"My Title";
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemCamera target: self action: @selector( whatEverMethod: )];

And you should see a right bar button and a title being set in your view controller.

--

What the "attributes inspector" => "top bar" does is to show you what the view will look like if it is embedded in a navigation controller. it does not actually add the navigation bar for you.

If you look closely, you will see that the "top bar" option is in a section called "simulated metrics. (They are just simulated, not really added to your view)

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