NavigationBar not loading when ViewController is programatically presented

469 Views Asked by At

I am attempting to load one navigation controller (BasicSearchController) from another (ViewController). In Storyboard, I have created 2 Navigation Controllers, which are not linked by segues, but are separate Nagivation controllers. Each is linked to ViewController and BasicSearchController respectively, and each view controller has its own title text in the navigation bar.

In ViewController.m my app, I have a navigation bar item which is presented using this code:

searchButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"search-icon.png"] landscapeImagePhone:[UIImage imageNamed:@"search-icon.png"] style:UIBarButtonItemStylePlain target:self action:@selector(presentSearch)];

When the button is pressed, this method is called:

- (void) presentSearch {
    NSLog(@"Search presented!");

    [self performSelector:@selector(pushBasicSearchController) withObject:self afterDelay:0.0];
    }

Finally, the selector runs this code:

- (IBAction)pushBasicSearchController {
BasicSearchController *basicController = [[BasicSearchController alloc] init];

[self presentViewController:basicController animated:YES completion:NULL];
}

In BasicSearchController, which is then presented, I have the following code to produce the user interface:

- (void) createInterface {

screenRect = [[UIScreen mainScreen] bounds];
screenWidth = screenRect.size.width;
screenHeight = screenRect.size.height;

UIImage *navBackgroundImage = [UIImage imageNamed:@"bar.png"];
[[UINavigationBar appearance] setBackgroundImage:navBackgroundImage forBarMetrics:UIBarMetricsDefault];

backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"background1.png"]];
[backgroundView setFrame:CGRectMake(0, 0, screenWidth, screenHeight)];
[backgroundView setContentMode:UIViewContentModeScaleToFill];
[[self view] addSubview:backgroundView];
[[self view] sendSubviewToBack:backgroundView];

underBarGradient = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"gradient-under-bar.png"]];
[underBarGradient setFrame:CGRectMake(0, 64, screenWidth, 3)];
[underBarGradient setContentMode:UIViewContentModeScaleToFill];
[underBarGradient setAlpha:0.5f];
[[self view] addSubview:underBarGradient];
[[self view] bringSubviewToFront:underBarGradient];

menuButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"menu.png"] landscapeImagePhone:[UIImage imageNamed:@"menu.png"] style:UIBarButtonItemStyleDone target:self action:@selector(presentMenu)];

navigationItemMain.leftBarButtonItem = menuButton;

searchButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"search-icon.png"] landscapeImagePhone:[UIImage imageNamed:@"search-icon.png"] style:UIBarButtonItemStylePlain target:self action:@selector(presentSearch)];

navigationItemMain.rightBarButtonItem = searchButton;

NSLog(@"UI created!");

}

underBarGradient and backGroundView are correctly shown. However, the NagivationBar and it's title text are not visible, and the navigation bar items are also missing.

Back in ViewController, I used the following code in AppDelegate.m to customise the navigation bar:

I have modified `ViewController`'s navigation bar's appearance using the following code, which is found in `AppDelegate.m`:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"bar.png"] forBarMetrics:UIBarMetricsDefault];
NSShadow* shadow = [NSShadow new];
shadow.shadowOffset = CGSizeMake(1.0f, 1.0f);
shadow.shadowColor = [UIColor clearColor];
[[UINavigationBar appearance] setTitleTextAttributes: @{
                                                        NSForegroundColorAttributeName: [UIColor whiteColor],
                                                        NSFontAttributeName: [UIFont fontWithName:@"Aliquam" size:20.0f],
                                                        NSShadowAttributeName: shadow
                                                        }];
return YES;
}

However, though this works for the navigation bar of ViewController, BasicSearchController's bar is not modified by this code.

My queries are:

  1. How can I customise BasicSearchController's navigation bar with the code used in AppDelegate?
  2. How can I make the navigation bar items appear in BasicSearchController?
2

There are 2 best solutions below

3
On BEST ANSWER

This is the issue:

- (IBAction)pushBasicSearchController {
    BasicSearchController *basicController = [[BasicSearchController alloc] init];

    [self presentViewController:basicController animated:YES completion:NULL];
}

You aren't pushing you are presenting. You need to push the view controller using:

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

That will solve the missing NavigationBar.

For customising a NavigationBar

You're better off customising an individual NavigationBar in the view controller .m file, preferably in the viewWillAppear method. You haven't specified what customisability you want so I will leave that your imagination.

2
On

I've solved it :) I changed

navigationItemMain.rightBarButtonItem = searchButton;

to

self.navigationItem.rightBarButtonItem = searchButton;

in the target view controller (basicSearchController). I also used the code

[basicController.navigationItem setHidesBackButton:YES];
[basicController.navigationItem setTitle:@"Find Duo Partners"];

immediately after pushing the controller in the code provided by SASmith, which hides the back button, and adds a title to the navigation bar. You can use this code in the target view controller instead, but it makes the buttons pop in, rather than appearing straight away.

Thanks for the help!