1

i have a table view that i want to make a custom uinavigationn bar i tried this code

UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, self.view.bounds.size.width, self.view.bounds.size.height)];
//here for v, width= navBar width and height=navBar height
//    
[view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"newsBanner.png"]]];
[self.navigationController.navigationBar addSubview:view];

the problem is the image is not centered cause its size is 640 x 72... is there a way to make it fit?

Eimantas
  • 48,927
  • 17
  • 132
  • 168
Mohamed Emad Hegab
  • 2,665
  • 6
  • 39
  • 64

1 Answers1

2

Your nav bar should only be 44 pixels tall. On top of that, it is maximum 320 points wide (640 px on retina devices, 320 on non-HD devices). So the size of your image is way too big. Make an image that has 320x44 dimensions, and then do this:

UINavigationBar *theBar = self.navigationController.navigationBar;
if ( [theBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)] ) {
    UIImage *image = [UIImage imageNamed:@"your image here"];
    [theBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
}

Of course that will only work on iOS 5. You'll need to find other ways of handling setting the background on OS below 5.0. But there are plenty of stack overflow questions that address that issue :)

anon_dev1234
  • 2,143
  • 1
  • 17
  • 33
  • As an alternative, you could probably scale your image in code, but that's no fun and probably a much bigger hassle than just making the image the correct size. – anon_dev1234 Jul 09 '12 at 20:07
  • there is a problem when you add a prompt to the navigationitem, the image is repeated, and we cannot stretch the image to the navigation bar. – djleop Dec 05 '12 at 13:51