Question

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
Was it helpful?

Solution

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

OTHER TIPS

Don't do it like Jojas. That is using internal class details that can change with an update of iOS. Use Appearance APIs on iOS 5.

Look here for something that works with both iOS 5 and iOS 4, Custom UINavigationBar Background

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top