PDA

View Full Version : Image as navigation bar: Issues




ildu
Feb 19, 2009, 08:17 AM
Hi everybody!

I'm very new to xCode and iPhone devving, so excuse my noobism :D.

I'm attempting to use an image to cover the entire navigation bar. The issue is that since I'm using titleView (and navigationItem), it's having problems covering the entire area of the nav bar, scaling the width down from the original size for whatever reason (Fig 1). In the case where I have a navigation bar button, it scales even smaller to make room for the button (Fig 2).

Fig 1: http://img403.imageshack.us/img403/8626/fig1gb3.png
Fig 2: http://img413.imageshack.us/img413/3499/fig2pn3.png

I need the image to cover the entire area of the navigation bar, and somehow get it to go under the nav bar button. The nav bar is initiated inside a tableView controller. Here's my code so far:


UIImage *image = [UIImage imageNamed:@"navBar.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = CGRectMake(0, 0, 320, 44);
self.navigationItem.titleView = imageView;


Is there a better way to do this than using titleView, or can this be remedied somehow without resorting to alternative approaches?



dejo
Feb 19, 2009, 09:31 AM
Is there a better way to do this than using titleView, or can this be remedied somehow without resorting to alternative approaches?
I think you want to avoid using the titleView but instead somehow manipulate the navigationItem or even the navigationBar. You sure you even need a background image for what you're trying to achieve? Wouldn't tintColor suffice?

ildu
Feb 20, 2009, 09:55 AM
I got it to work, but a few issues arose.

I took out the titleView command from the tableViewController, and did it the proper way using the navigationController. I added an imageView as a subview to the navigationController. I also shifted the back button init from the tableViewController to follow the image code.

An issue came up:
- The nav bar image now overlaps the back button. I've tried sending it to the back of the stack with sendSubviewToBack, but then it disappears as it apparently goes under the default nav bar. Should I put the button inside another subview and then send it to the front? And how might I do that?

Mi code ('theNavigationController' is my nav controller and 'dataSource' is where the image name is fetched):

// set image to navigation bar
UIImage *navImage = [dataSource navBarImage];
UIImageView *navImageView = [[UIImageView alloc] initWithImage:navImage];
[theNavigationController.view addSubview:navImageView];

// create a custom navigation bar button and set it to say the section name
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] init];
backButton.title = [dataSource name];
theNavigationController.navigationItem.backBarButtonItem = backButton;
[backButton release];

//[theNavigationController.view sendSubviewToBack:navImageView];


P.S. I do need the nav bar graphics to be images. The image changes with the tab bar selections, so tinting isn't enough for me. In the images that I posted, the actual nav bar image wasn't present, because I want to avoid copyright troubles :D.

dejo
Feb 20, 2009, 10:03 AM
Should I put the button inside another subview and then send it to the front? And how might I do that?
No need to put it in another subview. The button already is a subclass of UIView.

ildu
Feb 21, 2009, 12:10 PM
No need to put it in another subview. The button already is a subclass of UIView.

Ah, ok. So how do I send the button to the front? Would it then be like this: "[theNavigationController.view sendSubviewToFront:backButton];"?

I don't know if I already tried doing it that way (I don't have a Mac in front of me right now), but it sounds a bit funky since backButton is a navigationItem belonging to theNavigationController, and not necessarily a subview of the controller.

dejo
Feb 21, 2009, 06:57 PM
No need to put it in another subview. The button already is a subclass of UIView.
Oops, spoke to soon. It's not. Sorry.

ildu
Feb 23, 2009, 08:46 AM
Oops, spoke to soon. It's not. Sorry.

Ah, ok. So any ideas how I should tackle this? As a beginner, I'm not entirely versed in how navigationItems and views are handled together :).

jorgmart
Feb 25, 2009, 04:32 PM
Exactly same problem here ... :confused:

If I can get it work, I'll tell you ...

Regards