-1

I have a few navigation controllers that are set up in a NIB under a tab controller. I'm trying to set up the same logo in the top view controller of each navigationcontroller.

In the first view controller that appears, I have this code in viewDidLoad:

self.navigationItem.titleView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"green-noback-logo-only.png"]] autorelease];

This works (well, almost, I'll have to resize the image) and replaces the text set up in the NIB with my logo image.

However, this exact same code doesn't work in either of the other two view controllers. Instead, any text I've set up for the title in the NIB shows. I've tried putting that code in initWithCoder, viewDidLoad, viewDidAppear and viewWillAppear and it does nothing. I'm explicitly setting leftBarButtonItem to nil, although I'm guessing it was nil to begin with. I have also already checked that self.navigationItem is not nil in any of the places where I'm trying to set the titleView.

Any idea what would be special about the other two controllers that would prevent them from having a titleView set? Otherwise, does someone have a more foolproof way to set titleView?

Jesse Millikan
  • 3,104
  • 1
  • 21
  • 32

2 Answers2

1

try setting the image view to self.navigationController.navigationItem instead of self.navigationItem.

Srikar Appalaraju
  • 71,928
  • 54
  • 216
  • 264
Paul N
  • 1,901
  • 1
  • 22
  • 32
  • In fact, it looks like self.navigationController is nil in all of those methods. – Jesse Millikan Oct 31 '11 at 21:37
  • mmmm, try this: UIImage *image = [UIImage imageNamed:@"TableviewCellLightBlue.png"]; UIImageView *imageView = [[UIImageView alloc] initWithImage:image]; [self.navigationController.navigationBar.topItem setTitleView:imageView]; – Paul N Oct 31 '11 at 21:41
  • indeed you can see this link: http://stackoverflow.com/questions/1553118/adding-image-to-navigation-bar/1553383#1553383. In the question is the code I added before that works for setting the Title Image and in the answer is the code to set the image as a background of the navigation Bar. – Paul N Oct 31 '11 at 21:47
  • Like I said, self.navigationController is nil... So calling methods on it won't do anything. – Jesse Millikan Nov 01 '11 at 16:41
1

tl;dr: I screwed up initWithCoder:.

A good wallbanger for a beginner. Following Paul N's answer, I discovered that self.navigationController == nil in the two broken view controllers. It took me another few hours of head-desking to figure out the rest.

All three top level view controllers were subclasses of UITableViewController. However, only two of them were using grouped style. I was overriding initWithCoder: to use initWithStyle: inside the two non-working table view controllers. This threw away the connection to the navigation controller stored in the NIB. I originally did this because I couldn't figure out how to set grouped style on those inside the NIB (suggested by another answer here).

Serves me right for subclassing in such a rotten fashion, I guess.

Anyway, the solution was to fix the initWithCoder: implementation to call [super initWithCoder:coder] as usual and set up the table view style in the NIB. I did this by dragging a table view under that view controller, setting the datasource, setting the delegate, and setting it to grouped style. (This is how table view controllers are set up in the NIB by default.)

Community
  • 1
  • 1
Jesse Millikan
  • 3,104
  • 1
  • 21
  • 32