1

I have a NavigationController with customized UINavigationBar:

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

I'm allowing rotation using shouldAutorotateToInterfaceOrientation.

When i'm rotating, i would like to change the banner.png with banner_port.png

How can i do it?

Parth Bhatt
  • 19,381
  • 28
  • 133
  • 216
djTeller
  • 515
  • 2
  • 5
  • 14
  • 1
    overriding `-drawRect:` via a category is a *terrible* idea. The implementation of `UINavigationBar` is opaque, and you have no idea what's going on underneath. Making assumptions like this about how it's working is a good way to have code break in the future. – Dave DeLong Feb 28 '11 at 08:56

3 Answers3

0

[UIDevice currentDevice].orientation will tell you which way the device thinks it is...

you can do an if or switch at that point on the value it gives you.

amattn
  • 10,045
  • 1
  • 36
  • 33
0

You can make your navigation bar object to listen global UIDeviceOrientationDidChangeNotification, which has property 'orientation', and make necessary changes, when event occurs.

Ruslan Konyshev
  • 135
  • 1
  • 5
0

See also Custom UINavigationBar Background

I agree wit with Dave DeLong - overriding drawRect isn't wise, it may break some day. Plus, your category's drawRect replaces the original drawRect -- you don't have the option of calling super to get default functionality.

Note that if you listen for changes to UIDeviceOrientationDidChangeNotification, you need to be aware that there are six device orientations (the usual four, plus face up and face down).

Poorly written code is often seen which doesn't acknowledge the possibility of face up and face down device orientations.

N.B. you can also access the user interface's current status bar orientation - which isn't necessarily the same as the UIDeviceOrientation!

Community
  • 1
  • 1
occulus
  • 16,959
  • 6
  • 53
  • 76