Custom drawRect: in a subclass of UINavigationBar

I want a subclass UINavigationBarso that I can make a custom drawing in drawRect:.

Here is my code:

Navigation Controller (.h)

@interface CORENavigationController : UINavigationController

@property (strong, nonatomic) CORENavigationBar *customNavigationBar;

@end

Navigation Controller (.m)

@implementation CORENavigationController

@synthesize customNavigationBar = _customNavigationBar;

- (UINavigationBar *)navigationBar {
    if (![self customNavigationBar]) {
        [self setCustomNavigationBar:[[CORENavigationBar alloc] init]];
    }
    return [self customNavigationBar];
}

Navigation Bar (.h)

@interface CORENavigationBar : UINavigationBar

@end

Navigation Bar (.m)

@implementation CORENavigationBar

- (void)drawRect:(CGRect)rect
{
    [super drawRect:rect];
}

The problem is that there is no title in the navigation bar, for example. all i see is just a standard blue background.

As soon as I delete the following method, the header returns there:

- (UINavigationBar *)navigationBar {
    if (![self customNavigationBar]) {
        [self setCustomNavigationBar:[[CORENavigationBar alloc] init]];
    }
    return [self customNavigationBar];
}

Why does the name disappear? I call super drawRect:and do not change anything.

Thank!

+3
source share
1 answer

Found! Here is the answer:

fooobar.com/questions/66597 / ...

In this case, it will be:

[navigationController setValue:[[CORENavigationBar alloc] init] forKeyPath:@"navigationBar"];

Tested on iOS 5.1.1 (9B206). It works like a charm.

+3

All Articles