I have subclass of UINavigationBar
.
@interface MyNavigationBar : UINavigationBar
Made some changes and now want that my application NavigationController
would use it:
_navigationController = [[UINavigationController alloc] initWithRootViewController:self.viewController];
[_window addSubview:[_navigationController view]];
[self.window makeKeyAndVisible];
I want that _navigationController would have MyNavigationBar
How this could be done ?
Thanks.
You have to create a xib with a UINavaigationController
in it. You can then select the navigationBar
in Interface Builder and change the class to your subclass of UINavigationBar
.
Then to make this a little easier to instantiate I add a category to `UINavigationController like:
@interface UINavigationController (DSCNavigationController)
+ (UINavigationController *)dsc_navigationControllerWithRootViewController:(UIViewController *)rootViewController;
@end
@implementation UINavigationController (DSCNavigationController)
+ (UINavigationController *)dsc_navigationControllerWithRootViewController:(UIViewController *)rootViewController;
{
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"DSCNavigationController" owner:nil options:nil];
NSAssert(1 == [topLevelObjects count], @"DSCNavigationController should have one top level object");
UINavigationController *navigationController = [topLevelObjects objectAtIndex:0];
NSAssert([navigationController isKindOfClass:[UINavigationController class]], @"Should have a UINavigationController");
[navigationController pushViewController:rootViewController animated:NO];
return navigationController;
}
@end
At the top of the class that uses it makes sure to import the category in my case it looks like
#import "UINavigationController+DSCNavigationController"
Then using it looks something like
MyViewController *myViewController = [[MyViewController alloc] init];
UINavigationController *navigationController = [UINavigationController dsc_navigationControllerWithRootViewController:myViewController];
NSLog(@"%@", self.navigationController);
is not nul - Paul.s 2012-04-04 09:50
self.window.rootViewController = // ...
you should be setting it to the navigationController not the rootViewController of the navigationControlle - Paul.s 2012-04-04 10:28
UINavigationController has a read-only property
@property(nonatomic, readonly) UINavigationBar *navigationBar
since it is read-only you have to subclass UINavigationBar and override this property or make it read-write.E.g. :
MyNaviagtionBar *myBar = [[MyNavigationBar alloc] init];
_navigationController.navigationBar = mybar;
Or subclassing:
MyNavigationController.h
@class MyNavigationBar;
@interface MyNavigationController : UINavigationController
@property(nonatomic, strong) MyNavigationBar *navigationBar;
@end
MyNavigationController.m
@implementation MyNavigationController
@synthesize navigationBar = _navigationBar;
@end
And then change
_navigationController = [[UINavigationController alloc] initWithRootViewController:self.viewController];
to
_navigationController = [[MyNavigationController alloc] initWithRootViewController:self.viewController];
But when i try to do what you showed i get this: Assigment to read only propert - Streetboy 2012-04-04 08:11
Property 'navigationBar' attempting to use ivar '_navigationBar' declared in super class 'UINavigationController - Streetboy 2012-04-04 08:38