1

I have a Navigation Controller as the root of my app and I am using the appearance proxy to customise the look on iOS 5, but for iOS 4 I was hoping to use a category to override drawRect:, this was fine, except that all the Navigation bars were affected as you would expect from a category.

I don't want to tamper with the "system" popups, such as the Mail composer, or the SMS composer, I want their bars to stay blue and system-like.

I Tried to create my own UINavigationController with it's xib and change the class of the NavigationBar to my custom sub lass of UINavigationBar. But the results are not taking affect at all on screen.

I am aware of the following post but couldn't get any solutions to run as expected. How to subclass UINavigationBar for a UINavigationController programmatically?

My first attempt which does work but uses an undocumented setNavigationBar: method:

_myNavigationController = [[UINavigationController alloc] initWithRootViewController:someVC];

if([[BT_NavigationController class] respondsToSelector:@selector(appearance)]){

    // some iOS 5 magic in here !
    [_myNavigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"topbar.png"];

}else{

    // Probably looking at app store refusal 
    CustomBar * bar = [[CustomBar alloc] init];
    // [_myNavigationController setNavigationBar: bar];
    [bar release];

}

[parentView presentModalViewController:_myNavigationController animated:YES];

To avoid that I created a UINavigationController, which I also had a xib for, reassigning the class of the navigation bar to my custom class, but placing breakpoints in drawRect: method, I can see that this isn't being called.

Why would that be, it seems that my code is not loading the nib, and therefore not realising the nab bar should be my custom class and not the UINavigationBar.

Any tips would be helpful, thanks.

Community
  • 1
  • 1
Daniel
  • 23,129
  • 12
  • 109
  • 154

1 Answers1

2

If your modification is just about adding an image, you can simply insert it from the view controllers you want to customize:

UIImageView *bgImageView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"topbar.png"]] autorelease];
[self.navigationController.navigationBar addSubview:bgImageView];

You have to tweak a little based on what other elements you have in your UINavigationBar for your UIImageView to be at the lowest index of the subviews but still above the background. Worked for us.

Good luck.

teriiehina
  • 4,741
  • 3
  • 41
  • 63
  • It seems that when I set this for my Nav controller, subsequent detail views's bars title and bar buttons are placed behind the image. – Daniel Feb 22 '12 at 14:48
  • yep. you have to use the sendSubviewToBack: (or the insertSubview:atIndex:) on the navigationBar. It depends on the subviews of your navigationBar. – teriiehina Feb 22 '12 at 15:24