Question

I'm adding a custom background image to my UINavigationBar by overriding the drawRect via a category.

- ( void )drawRect:( CGRect )rect{
    [[UIImage imageNamed:@"navbar.png"] drawInRect:CGRectMake( 0, self.frame.size.height-44, self.frame.size.width, 44 )];
}

This solution seems to work fine, until you try to make use of navigationitem.prompt property. Instead of doing a smooth animation of presenting the prompt, it does it abruptly.

Are there any suggestions on how to remedy this ? Or an alternative way of setting a background image.

PS. I'm trying to avoid just adding a background image as a subview because of the way things rearrange when views push/pop. And I'd hate to sendSubviewToBack in every view's viewDidAppear.

TIA!

EDIT: I also tried method swizzling, which would work fine if I could just let my image stretch, but since the image isn't stretchable, I need to figure out how to tap into the animation that happens to do a shift instead of a stretch.

EDIT: see my hacky answer below

Was it helpful?

Solution

So very "hacky", but this is the closest I can get it to do what I want...

Basically when method swizzling, simply letting the image stretch wasn't working well for me because I also had rounded corners. So instead I made the corners a separate transparent img and add it as a subview so they wouldn't get affected by the stretch animation.

So my category now looks like this..

#import "UINavigationBar+custom.h"

#define cornersTag 1

@implementation UINavigationBar (UINavigationBarCategory)

-(void)setCornersIfNeeded{
 UIImageView *corners = (UIImageView*)[self viewWithTag:cornersTag];
 if(!corners){
  UIImage *img = [[UIImage imageNamed:@"navbar_corners.png"] stretchableImageWithLeftCapWidth:10 topCapHeight:0];
  corners = [[[UIImageView alloc] initWithImage:img] autorelease];
  corners.frame = CGRectMake(0, 0, self.frame.size.width, img.size.height);
  corners.tag = cornersTag;
  [self addSubview:corners];
 }
}

- (void)customDrawRect:( CGRect )rect{
 [self customDrawRect:rect];
 [[UIImage imageNamed:@"navbar.png"] drawInRect:rect]; 
 [self setCornersIfNeeded];
}

@end

for more on method swizzling... http://www.cocoadev.com/index.pl?MethodSwizzling and Method Swizzle on iPhone device

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top