0

Im customizing my navbars so they all have a logo in them. I want to add the logo right around here:

Image in Navbar

I know navbars have a background image property but that would stretch it to the whole navbar. I just want it there! :)

So far Ive tried this:

UIImage *gradientImage44 = [[UIImage imageNamed:@"NavBar25High"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
    UIImage *gradientImage32 = [[UIImage imageNamed:@"NavBar25High"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
    // Set the background image for *all* UINavigationBars
    [[UINavigationBar appearance] setBackgroundImage:gradientImage44 forBarMetrics:UIBarMetricsDefault];
    [[UINavigationBar appearance] setBackgroundImage:gradientImage32 forBarMetrics:UIBarMetricsLandscapePhone];

But thats for gradients because as I mentioned, it uses the image as a background image :(

marciokoko
  • 4,988
  • 8
  • 51
  • 91

2 Answers2

1

I would allocate an ImageView using CGRectMake to the specific location you want it and add that image view to the view of the UINavigationController.

Like this:

Declare the image

UIImage *myImage =[UIImage imageNamed:@"header.png"];

Allocate the image view

myImageView_tools = [[UIImageView alloc] initWithFrame:CGRectMake(97.5, 20, 125, 45)];

Set the image as the image of the imageview

myImageView_tools.image = myImage;

Add this to your navbar.

[nameofyournavbar.view addSubview:myImageView_tools];

In my code I happened to add it to the RootViewController instead of the NavigationBar but I would imagine it would work just the same.

logixologist
  • 3,694
  • 4
  • 28
  • 46
0

How about adding the image as the custom view of a UIBarButtonItem. Maybe using a UIBarButtonSystemItemFixedSpace item with a specified width.

Wain
  • 118,658
  • 15
  • 128
  • 151
  • I thought those were only for uitoolbars? – marciokoko Jun 14 '13 at 00:04
  • http://stackoverflow.com/questions/2504351/how-to-add-a-button-to-uinavigationbar – Wain Jun 14 '13 at 06:33
  • Yes but that post adds buttons to navbars. That I can do. I would need to add something like a fixed space but fixed spaces are only for uitoolbars. That's ok, I just made an image to use as gradient for the navbar :) – marciokoko Jun 14 '13 at 12:20
  • I can't say I've tried it but the documentation says "Items can include fixed-width and flexible-width spaces." – Wain Jun 14 '13 at 14:23