how to change navigation bar background image manually in specific ViewController?
i'm trying to change the image background of my navigationbar when i'm in specific ViewController. My app model : - appDelegate - tabbarcontroller -viewcontroller1 -navigationBarController -viewcontroller2 -viewcontroller3 (*) -viewc开发者_JAVA技巧ontroller4 -viewcontroller5
I've already implemented this code in appDelegate :
static NSMutableDictionary *navigationBarImages = NULL;
@implementation UINavigationBar(CustomImage)
+ (void)initImageDictionary
{
if(navigationBarImages==NULL){
navigationBarImages=[[NSMutableDictionary alloc] init];
}
}
- (void)drawRect:(CGRect)rect
{
UIColor *color = [UIColor blackColor];
NSString *imageName=[navigationBarImages objectForKey:[NSValue valueWithNonretainedObject: self]];
if (imageName==nil) {
imageName=@"navbar.png";
}
UIImage *image = [UIImage imageNamed:imageName];
[image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
self.tintColor = color;
}
- (void)setImage:(UIImage*)image
{
[navigationBarImages setObject:image forKey:[NSValue valueWithNonretainedObject: self]];
}
@end
after that, i wrote this code in my viewcontroller2 :
[UINavigationBar initImageDictionary];
it's work, my navigation bar is like i wished, however in my viewcontroller3, i wrote :
UIImage *navBarLandscape = [UIImage imageNamed:@"navbar.png"];
[[[self navigationController] navigationBar] setImage:navBarLandscape];
and it doesn't do the trick, i have an error that i can't undertand :
[UIImage length]: unrecognized selector sent to instance 0x5a58130 2010-11-18 18:02:37.170 finalAudi[1463:307] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIImage length]: unrecognized selector sent to instance 0x5a58130'
Do you have an idea?
regards polo.
In each viewController's viewDidLoad
try:
/* make sure that we set the image to be centered */
/* you will ahve to make minor adjustments depending on the dimensions of your image. */
UIImageView *logo = [[UIImageView alloc] initWithFrame:CGRectMake(self.navigationController.navigationBar.frame.size.width/2-75,0,150,
self.navigationController.navigationBar.frame.size.height-1)];
[logo setImage:[UIImage imageNamed:@"myImage.png"]];
[self.navigationController.navigationBar addSubview:logo];
[self.navigationController.navigationBar sendSubviewToBack:logo];
and for viewWillAppear:
and viewDidDisappear:
try:
/* This will ensure that if you have a different image for each view that the previous viewController's navBar image wont stay. */
-(void)viewWillDisappear:(BOOL)animated {logo.hidden = YES;}
-(void)viewWillAppear:(BOOL)animated {logo.hidden = NO ;}
and to make sure that the image stays centered when rotating:
-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
if(((interfaceOrientation == UIInterfaceOrientationLandscapeLeft) ||
(interfaceOrientation == UIInterfaceOrientationLandscapeRight))){
[logo setFrame:CGRectMake(self.navigationController.navigationBar.frame.size.width/2-75,0,150,self.navigationController.navigationBar.frame.size.height-1)];
}else if(((interfaceOrientation == UIInterfaceOrientationPortrait) ||
(interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown))){
[logo setFrame:CGRectMake(self.navigationController.navigationBar.frame.size.width/2-80,0,150,self.navigationController.navigationBar.frame.size.height-1)];
}
}
thank's to all of you, i was able to fix my problem with this code in appdelegate:
@implementation UINavigationBar(CustomImage)
+ (void)initAppDelegate
{
if (applicationDelegate==Nil) {
applicationDelegate = (finalAudiAppDelegate *)[[UIApplication sharedApplication] delegate];
}
}
- (void)drawRect:(CGRect)rect
{
UIColor *color = [UIColor blackColor];
[[[applicationDelegate backNavBar] objectAtIndex:[applicationDelegate indexBackNavBar]] drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
self.tintColor = color;
}
@end
i'm using one NSMutableArray to stock my navigationBar's background images and one NSInteger (indexBackNavBar), i'm access to them with UIApplication sharedApplication. Then in my viewcontroller, i just set indexBackNavBar = 1 in viewWillAppear and indexBackNavBar = 0 in viewWillDisappear. Voila!
If I understand correctly, I would recommend looking at the UINavigationBarDelegate and implementing the code to switch the background of the navigation bar there, instead of spreading it across multiple viewControllers.
The delegate has methods that will get called before pushing and popping new views so it might be better from a design perspective
精彩评论