NavigationBar not loading when ViewController is programatically presented

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?

--------------Solutions-------------

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.

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!

Category:ios Time:2018-12-12 Views:2

Related post

  • Google Maps - sometimes tiles not loading when dragging 2011-08-11

    In general my Google Maps API V3 / Javascript application is working fine. I only have noticed one issue: From time to time - I can not really tell when - the (new) tiles are not loaded when I drag the map. This is a bit disturbing. Since my applicat

  • crystal reports viewer does not load when asp.net application is deployed to server 2012-03-08

    On my local build, when I open the page that has the crystal reports viewer - everything loads and looks fine (the crystal reports viewer actually shows up on the page). when I copied this over to the server for the web (and installed the CR framewor

  • jqGrid dataUrl not loading when grid is loaded 2012-04-11

    I am using inlineNav (successfully due to assistance here!). Now using dataUrl in a select statement, I do not understand the behavior. The select options are not loaded when the grid is loaded. The select HTML request is not made until either edit o

  • PERSONAL.XLS does not load when Excel starts. 2012-06-02

    PERSONAL.XLS does not load when Excel starts. --------------Solutions------------- If you want personal.xls to load whenever Excel is started, copy the personal.xls workbook and paste it in the xlstart folder. To perform the same follow the link belo

  • windows live mail not loading when I click the mail icon in windows live messenger. XP 2013-03-12

    windows live mail not loading when click mail icon in windows live messenger. Webmail (mozilla/internet explorer) runs instead. --------------Solutions------------- Please submit all Live and Hotmail queries to the appropriate forum here: Windows Liv

  • windows 10 startups are not loading when I change the start menu as windows 8 type 2012-09-23

    windows 10 startups are not loading when I change the start menu as windows 8 type --------------Solutions------------- windows 10 startups are not loading when I change the start menu as windows 8 type I have also experienced this on my systems. It

  • Outlook.com not loading when I try to compose a message 2013-12-10

    Now after reading similar questions about Outlook.com problems and trying the methods suggested. I found out my issue was when I went to compose an email. Now though I do not mind seeing the very attractive female moderators on this page, I think if

  • GMAIL does not load when using IE9 2013-12-25

    I am using an Intel Core i7 with 8 GB and Windows 7x64. For months I had no problem loading my GMAIL, it worked like I expected. But all of a sudden my GMAIL will not load when I am using IE9. If I click on Load Basic HTML it will load. If I use Fire

  • Internet Explorer will not load when the button is pressed. 2014-06-22

    Internet Explorer will not load when the button is pressed. I can start it by clicking "Run as administrator" and this starts I.E 10. On first starting the computer I get a message stating that I need to run I.E4 or later. I have tried to uninstall I

  • Why have I started to get an error message " Prop Res Dll Not Loaded " when I attempt to print form several programs? Is this a registry file problem? 2014-11-07

    Why have I started to get an error message " Prop Res Dll Not Loaded " when I attempt to print form several programs? The ERROR Message usually happens when printing a document that includes a graphic or picture in it. Is this a registry file problem

  • CSS is not loading when page is refreshed 2014-12-04

    my webpage is not loading when page is refreshed but second time it is loading css and after another page refresh is not loading css and so on. would any budy will help me for this issue because i am working on this issue from last two days. --------

  • Why CSS files not loading when compressing HTTP response using GZipStream? 2009-02-21

    I am developing an application using asp.net 2.0 (C#), in which I am trying to implement the compression of my files, so that performance of my website will improve. For that I have added a code in my Global.asax file to compress all requests (.aspx,

  • yui_editor not loaded when loading the partial via an ajax call 2010-08-19

    In a Rails app, I am loading a partial via an ajax call. (still using prototype) The partial is a form that contains a textarea enriched with the yahoo yui_editor (similar to tinyMCE or FCKEditor) <%= f.text_area :body, :class => 'rich_text_edi

  • Google charts not loading when page is requested via ajax call 2011-03-13

    I am trying to use google charts on my page to display stuff from google analytics. I have split my analytics reports into more sections: Pages, Browsers, Operating Systems, Traffic sources etc... I have a handler which gets my request and performs a

  • VSTO Addin does not load when Document created from Template and VSTO Customization Error, WHY? 2011-03-18

    i have created a VSTO addin in VS 2008 for Office 2007. it loads perfectly when Word is started as a standalone. i have another application that starts a Word document, using a template and fills up the required details in the template. The problem i

  • Google Map is not loading when i use progaurd 2012-02-20

    In my application project.properties i have added proguard.config=proguard.cfg to use progaurd. And i have imported the apk file like --> export -->export android application -->create new keystore so on.... And created apk file, when i run

  • Qt DLL not loading (when using Qwt inside) 2012-03-05

    I am trying to use Qwt in one of my Qt DLLs. The thing is that the library does not load if I call a constructor of QwtPlot inside. If I comment it out it loads. Just to note that DLL builds successfully when QwtPlot is uncommented. I am using Visual

  • jQuery Mobile: Dynamic content not loading when I return to a page 2012-03-08

    My data from getmovies.php is working correctly and loading into #moviesPage the FIRST time I load the page. However, if I navigate away from #moviesPage and then return to it the content does not reload. The header and footer appear and I can see th

  • Internet Explorer will not load when I log into my user name. 2012-04-30

    I have 5 user names on Windows XP. When I log onto my account and click on Internet Explorer it will not load. The hourglass stays there for ages and I cannot go anywhere else so I end up switching off the computer and logging on to one of the other

Copyright (C) pcaskme.com, All Rights Reserved.

processed in 0.861 (s). 14 q(s)