Reputation: 31486
I want to subclass UINavigationBar
so that I can do custom drawing in its drawRect:
.
Here's 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 issue is that the navigation bar has no title, e.g. all I can see is just the standard blue background.
As soon as I remove the following method, the title is back there:
- (UINavigationBar *)navigationBar {
if (![self customNavigationBar]) {
[self setCustomNavigationBar:[[CORENavigationBar alloc] init]];
}
return [self customNavigationBar];
}
Why does the title disappear? I call the super
's drawRect:
and do not change anything else.
Thanks!
Upvotes: 3
Views: 1246
Reputation: 31486
Found it! Here's the answer:
https://stackoverflow.com/a/9610801/1306956
In this case, it'd be:
[navigationController setValue:[[CORENavigationBar alloc] init] forKeyPath:@"navigationBar"];
Tested on iOS 5.1.1 (9B206). Works like a charm.
Upvotes: 3