views:

487

answers:

2

Hi,

I created an application with a TabBarController, 4 TabBarItems and every TabBarItem have is own NavigationController file (everything created in IB).

I'm using the drawRect function to design my navigationBar:



@implementation UINavigationBar (customImage)

-(void)drawRect:(CGRect)rect {

  UIImage *image = [UIImage imageNamed:@"MyImage.png"];
  [image drawInRect:CGRectMake(0,0, self.frame.size.width,self.frame.size.height)];

}

@end

The problem is that the NavigationBar change for every ViewController inside my app, what I want is only draw the NavigationBar for one TabBarItem only and use the default navigationBar for other controller.
How to do that?

Thanks,

A: 

Using a category is probably not the best idea in this case (if you override drawRect: using a category, all UINavigationControllers will be affected). You'd be better off subclassing UINavigationController, and using the custom subclass only in the tab that you want.

Edit: Subclassing UINavigationController is fairly easy:

(in the .h file)

@interface MyCustomNavigationController : UINavigationController {
}
@end

(in the .m file)

@implementation MyCustomNavigationController

-(void)drawRect:(CGRect)rect {
    //custom drawing here
}
@end

Then, use MyCustomNavigationController instead of UINavigationController in your code where appropriate. Categories (which you used in the sample) are good for adding convenience methods to existing classes, but can be dangerous when replacing already-existing methods (since your custom method will replace the original for all uses).

eman
ok but how can I subclass the NavigationController and draw the navigationBar inside? I think thats the best idea but I don't knwo how to do it. Do you have some code?
ludo
I already do that, because like I said I use IB and I have subclass every navigationController like this. but it seems that the drawRect is not called.
ludo
Oh, sorry, I didn't see that you were adding a category to UINavigationBar, rather than UINavigationController (`drawRect:` won't be called for UINavigationController). Unfortunately, UINavigationBar and UINavigationController aren't very flexible in this regard (you can't set the navigation bar of a navigation controller). You might be better off using a custom UIToolbar (I found a quick tutorial at http://www.iphonesdkarticles.com/2008/09/navigation-controller-uitoolbar.html). Otherwise, you can try adding the image as one of the items of the navigation bar.
eman
Thank you for your help, but I think I can't use a UIToolBar, I need in each of my ViewController a custom UInavigationBar. there must be a way to draw the NavigationBar for each of them, I have to fint it
ludo
A: 

Ok,

He take me a long time but I fins a solution. by using swizzling method I can do whatever I want. Look at this link for more information on how to implement it.

ludo
any chance you could post the actual code you used to swizzle the drawRect method for a particular controller?
prendio2