2010-11-18 5 views
0

Ich versuche den Bildhintergrund meiner Navigationsleiste zu ändern, wenn ich in einem bestimmten ViewController bin. Meine App-Modell: - AppDelegate - TabBarController -viewcontroller1 -navigationBarController -viewcontroller2 -viewcontroller3 (*) -viewcontroller4 -viewcontroller5Wie man Navigationsleiste Hintergrundbild manuell in bestimmten ViewController ändern?

ich bereits diesen Code in AppDelegate implementiert haben:

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) { 
      [email protected]"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 

danach schrieb ich diesen Code in meinem Viewcontroller2:

[UINavigationBar initImageDictionary]; 

es ist Arbeit, meine Navigationsleiste ist, wie ich wollte, aber in meinem viewcontroller3, ich schrieb:

UIImage *navBarLandscape = [UIImage imageNamed:@"navbar.png"]; 
    [[[self navigationController] navigationBar] setImage:navBarLandscape]; 

und es macht nicht den Trick, ich habe einen Fehler, der i undertand nicht:

[UIImage Länge]: unerkannte Selektor Instanz gesendet 0x5a58130 2010-11-18 18: 02: 37,170 finalAudi [1463: 307] * Beenden app aufgrund abgefangene Ausnahme 'NSInvalidArgumentException', Grund: ‚- [ UIImage length]: unerkannter Selektor gesendet an Instanz 0x5a58130 '

Haben Sie eine Idee?

grüße polo.

Antwort

2

in jedem viewDidLoad Versuch des Viewcontroller:

/* 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]; 

und für viewWillAppear: und viewDidDisappear: Versuch:

/* 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 ;} 

und sicher zu stellen, dass das Bild zentriert bleibt beim Drehen:

-(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)]; 
    } 
} 
0

Wenn ich richtig verstehe, würde ich empfehlen Beenden Sie den Blick auf die UINavigationBarDelegate und implementieren Sie den Code, um den Hintergrund der Navigationsleiste dort zu wechseln, anstatt es über mehrere ViewControllers zu verbreiten.

Der Delegierte hat Methoden, die vor dem Drücken und knallen neue Ansichten genannt wird erhalten, so dass es aus einer Design-Perspektive besser sein könnte

1

Danke an euch alle, ich war in der Lage, mein Problem mit diesem Code in AppDelegate zu beheben:

@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 

ich verwende ein NSMutableArray meiner navigationbar die Hintergrundbilder und ein NSInteger (indexBackNavBar) Lager, ich bin der Zugang zu ihnen mit UIApplication shared. Dann in meinem Viewcontroller, ich setze nur indexBackNavBar = 1 in viewWillAppear und indexBackNavBar = 0 in viewWillDisappear. Voila!

Verwandte Themen