IPhone UINavigation bar background

I applied the following code to my application to change the image in the navigation bar.

- (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [self.navigationController.navigationBar setTintColor:[UIColor blackColor]]; [self setNavigationBarTitle]; } -(void)setNavigationBarTitle { UIView *aViewForTitle=[[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 45)] autorelease]; UIImageView *aImg=[[UIImageView alloc] initWithFrame:CGRectMake(-8, 0, 320, 45)]; aImg.image=[UIImage imageNamed:@"MyTabBG.png"]; [aViewForTitle addSubview:aImg]; [aImg release]; UILabel *lbl=[[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 305, 45)] autorelease]; lbl.backgroundColor=[UIColor clearColor]; lbl.font=[UIFont fontWithName:@"Trebuchet MS" size:22]; lbl.shadowColor=[UIColor blackColor]; [lbl setShadowOffset:CGSizeMake(1,1)]; lbl.textAlignment=UITextAlignmentCenter; lbl.textColor=[UIColor whiteColor]; lbl.text=@ "Mobile Tennis Coach Overview"; [aViewForTitle addSubview:lbl]; [self.navigationItem.titleView addSubview:aViewForTitle]; } 

See the following images. You can see the problem I am facing.

alt text


alt text

Each view controller in my application has the above methods for adjusting the background of the navigation bar.

Be that as it may, when I push the new view controller into my application. The back button will appear.

I need a return button to display. But the image should be behind the back button.

Now I'm a little confused here.

Can you help me with this?

Thank you in advance for sharing your knowledge with me.

Many thanks.

+6
iphone xcode uinavigationitem uinavigationbar
source share
2 answers

After an annoying night, I found a little tweak if you use drawLayer. With drawRect, when you play a video or video on YouTube, the navigation bar is replaced by an image. And I read a few posts that this caused a rejection of their application.

 @implementation UINavigationBar (UINavigationBarCategory) - (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx { if([self isMemberOfClass:[UINavigationBar class]]) { UIImage *image = [UIImage imageNamed:@"navBarBackground.png"]; CGContextClip(ctx); CGContextTranslateCTM(ctx, 0, image.size.height); CGContextScaleCTM(ctx, 1.0, -1.0); CGContextDrawImage(ctx, CGRectMake(0, 0, self.frame.size.width, self.frame.size.height), image.CGImage); } else { [super drawLayer:layer inContext:ctx]; } } @end 

If this article is accurate, everything should be fine with this approach: http://developer.apple.com/iphone/library/qa/qa2009/qa1637.html

+6
source share

The short answer is that changing the structure of the UINavigationBar is not supported by Apple. They really do not want you to do what you are trying to do. This is what causes the problem you are seeing.

Please submit a radar requesting this function so that it can get enough attention so that it is officially added at some point.

Having said that, to solve the problem, you can add a category to the UINavigationBar using the -drawRect: method and draw a background image in this method. Something like this will work:

 - (void)drawRect:(CGRect)rect { static UIImage *image; if (!image) { image = [UIImage imageNamed: @"HeaderBackground.png"]; if (!image) image = [UIImage imageNamed:@"DefaultHeader.png"]; } if (!image) return; CGContextRef context = UIGraphicsGetCurrentContext(); CGContextDrawImage(context, CGRectMake(0, 0, self.frame.size.width, self.frame.size.height), image.CGImage); } 
+5
source share

All Articles