Clarence
Clarence

Reputation: 1943

custom UINavigation bar in iOS5

I am having troubles after updating to Xcode 4.2. Before I used the following codes for making custom navigation bar, but when i use iPhone 5.0 simulator, it fails whereas in iPhone 4.2 simulator it was ok.

May I know what is the problem and how can i fix this?

Many thanks

@implementation UINavigationBar (UINavigationBarCustomDraw)
- (void) drawRect:(CGRect)rect {

   [self setTintColor:[UIColor colorWithRed:0.4f
                                    green: 0.0f
                                     blue:0.4f 
                                    alpha:1]];

   if ([self.topItem.title length] > 0 && ![self.topItem.title isEqualToString:@""]) 
   {
        [[UIImage imageNamed:@"purple.jpg"] drawInRect:rect];   
   }
}

@end

Upvotes: 2

Views: 1271

Answers (1)

Sakares
Sakares

Reputation: 606

If you need custom UINavigationbar with some image so you need to put this code in rootViewController that is first view of navigate stack (A > B > C , so you have to put this in A)

- (void)viewDidLoad
{
  [super viewDidLoad];

  [[UIDevice currentDevice] systemVersion];

  if ([[[UIDevice currentDevice] systemVersion] floatValue] > 4.9) {

      //iOS 5
      UIImage *toolBarIMG = [UIImage imageNamed: @"purple.jpg"];  

      if ([self.navigationController.navigationBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)]) { 
         [[UINavigationBar appearance] setBackgroundImage:toolBarIMG forBarMetrics:UIBarMetricsDefault]; 
      }

      } else {

      //iOS 4
      [self.navigationController.navigationBar insertSubview:[[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"purple.jpg"]] autorelease] atIndex:0];  
       }
   }
} 

Upvotes: 2

Related Questions