Search code examples
iosuinavigationcontrolleruinavigationbar

my UINavigatioBar is not on the top of view - programmatically


I am creating a UINavigationBar using the following,

    UINavigationController *navigationController = [[UINavigationController alloc] init];
    [view addSubview: navigationController.view];

How come it shows up like this?

simulator
(source: gyazo.com)

I want it to be on the top of the view but there is space there, why?

Thanks.


Solution

  • You are not creating things correctly. You should create your view controller, then create a navigation controller, passing the view controller as the nav controller's root controller. Then you make the nav controller the app's root controller.

    UIViewController *vc = ... // create your view controller
    UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:vc];
    window.rootViewController = nc;
    

    The code you posted does not create a nav bar, it creates an entire navigation controller.