UINavigationController (drawRect:)

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)];



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?



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 {

(in the .m file)

@implementation MyCustomNavigationController

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

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).


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.

Need Your Help

Why new fb api 2.4 returns null email on MVC 5 with Identity and oauth 2?

c# facebook facebook-graph-api asp.net-mvc-5 asp.net-identity-2

Everything used to work perfect until fb upgraded it's api to 2.4 (I had 2.3 in my previous project).

Pass value scope to another

jquery html5 angularjs

For now, i have a controller which allow to get data (json) from database, with a search input.