0
votes

This is really weird and I can't seem to be able to explain that. Took me hours to realize that only when I add a UIImageView as a subView to my self.view in the ViewController viewDidLoad then I can set the tint color of my navbar. If I add the UIImageView in the init method - tint color doesnt do anything to my nav bar.

So...

In this case my nav bar is getting the tint color:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
    // Custom initialization
}
return self;
}


- (void)viewDidLoad
{
[super viewDidLoad];

//NAV BAR COLOR  
self.navigationController.navigationBar.tintColor = [UIColor blueColor];

//ADDING BACKGROUND IMAGE 
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
imgView.image = [UIImage imageNamed:@"aaa.png"];
[self.view addSubview: imgView];
[self.view sendSubviewToBack:imgView];

}

Here it doesnt (just moved the adding of the image to the init method):

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
  //ADDING BACKGROUND IMAGE 
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
imgView.image = [UIImage imageNamed:@"aaa.png"];
[self.view addSubview: imgView];
[self.view sendSubviewToBack:imgView];    }
return self;
}


- (void)viewDidLoad
{
[super viewDidLoad];

//NAV BAR COLOR  
self.navigationController.navigationBar.tintColor = [UIColor blueColor];

}

This is how I call this view:

Login *loginViewController = [[Login alloc] initWithNibName:nil bundle:nil];
UINavigationController *navCntrl1 = [[UINavigationController alloc] initWithRootViewController:loginViewController];
[self presentModalViewController:navCntrl1 animated:YES];

Any idea why? I am soo puzzled...thanks!!

1

1 Answers

1
votes

It's nothing to do with your image, it's to do with the way UINavigationControllers are created.

Here's your code (with some comments added) :

// Create the view controller
Login *loginViewController = [[Login alloc] initWithNibName:nil bundle:nil];

// Create a navigation controller and add it
UINavigationController *navCntrl1 = [[UINavigationController alloc] initWithRootViewController:loginViewController];

viewDidLoad is called the first time that the ui needs to know about your view controller's view. If your viewDidLoad method wants the edit a feature of a navigation controller then _your view controller has to be added to the navigation controller before viewDidLoad is called`.

In your first example, viewDidLoad was called when you added your view controller to a navigation controller (the second line above).

In your second example, viewDidLoad was called when you said self.view in the initWithNibNamed:bundle: method. Crucially, this is before your view controller knows that it's going to be added to a navigation controller. Therefore when you say self.navigationController it won't know which navigation controller you are talking about. It can't set the tint of something it doesn't know about yet!

In your first example, viewDidLoad is called after you have added your view controller to a navigation controller so when you say self.navigationController it knows which navigation controller you are talking about and can set the tint accordingly.

To test this yourself, put NSLog(@"%@", self.navigationController); in your viewDidLoad method - I bet it outputs nil for the second example ;)


FYI :

You should always be doing the first example anyway - it will only create the image when it's needed and will let you unload the image in cases of low memory.