0
votes

EDIT 2: I have now fixed this issue. I was setting a custom background image for my UINavigationBar like so:

UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 44.0)];
[imgView setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"NavigationBarBackground" ofType:@"png"]]];
[navBar addSubview:imgView];
[navBar sendSubviewToBack:imgView];
[imgView release];

Which was fine for the first view controller on the UINavigationController's view controller stack, but any sub views were making this code re-run and placing the custom background to the front.

The Fix: Simply remove all your code for the custom background on the navigation controller, then EXTEND the UINavigationBar class in the following way:

Create a new .h / .m file called UINavigationBar+CustomImage. Set the .h code to:

@interface UINavigationBar (CustomImage)
- (void)drawRect:(CGRect)rect;
@end

And the .m to:

#import "UINavigationBar+CustomImage.h"

@implementation UINavigationBar (CustomImage)
- (void)drawRect:(CGRect)rect {
    UIImage *image = [UIImage imageNamed: @"NavigationBarBackground.png"];
    [image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
@end

And voila! Everything works fine now.

--- END EDIT 2 ---

I have a UINavigationController, which drills down like so:

HOMEPAGE > DETAIL PAGE > MAP VIEW

The DETAIL PAGE adds a custom UIToolbar (with three buttons) to the rightBarButtonItem within viewDidLoad like so:

UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 1.0, 157.0, 44.1)];
[imgView setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"NavigationBarBackground" ofType:@"png"]]];
[tools addSubview:imgView];
[tools sendSubviewToBack:imgView];
[tools setTintColor:[UIColor colorWithRed:127/255.0 green:184/255.0 blue:72/255.0 alpha:1.0]];
[imgView release];

NSMutableArray* buttons = [[NSMutableArray alloc] initWithCapacity:3];

UIBarButtonItem* bi = [[UIBarButtonItem alloc] initWithTitle:@"Filter" style:UIBarButtonItemStyleBordered target:self action:NULL];

[buttons addObject:bi];
[bi release];

bi = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"Map.png"] style:UIBarButtonItemStyleBordered target:self action:@selector(showMap:)];

[buttons addObject:bi];
[bi release];

bi = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"Favourite.png"] style:UIBarButtonItemStyleBordered target:self action:NULL];
[buttons addObject:bi];
[bi release];

[tools setItems:buttons animated:NO];

[buttons release];

self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:tools];

This displays perfectly when viewing the DETAIL VIEW page, and when I go to the MAP VIEW, the custom UIToolbar disappears (as expected), however, when going back to the DETAIL VIEW, the custom UIToolbar is no longer visible. It is there, though, as I can still tap the area where the 'Map' button should be, and it pushes me back to the MAP VIEW.

Would you know how I can get this UIToolbar to display again when going back to the DETAIL view?

I have tried rebuilding the custom UIToolbar in viewWillAppear, which has the exact same effects as above.

EDIT - This works if I put the code that builds the custom UIToolbar within the viewDidAppear method. I don't like this solution though, as the view slides back, then the custom rightBarButtonItem appears. Is this something to do with the navigation controller not being initalised before the page is displayed?

2

2 Answers

1
votes

If it works in viewDidAppear you could try viewWillAppear. In general, I've never seen a UIToolbar set as a UIBarButtonItem, so I wouldn't necessarily expect standard behavior. Let me know how it goes.

0
votes

Apple UI APIs often fail in unusual ways when you try to do anything even slightly unusual (even things that should work), and trying to insert a UIToolbar into a rightBarButtonItem definitely falls into my definition of "unusual".

What you really should be doing is hiding the navigation bar and adding the UIToolbar as a subview of your view controller's main view. Alternatively you could avoid UIToolbar completely and implement the buttons yourself in a custom view.