How to use custom UINavigationBar

Go To StackoverFlow.com

0

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.

2012-04-04 06:33
by Streetboy


4

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.

enter image description here

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];
2012-04-04 08:54
by Paul.s
Could you show me how it should look in AppDelegate - Streetboy 2012-04-04 09:12
Worked. But this don't work the [self.navigationController pushViewController:sendMsg animated:YES] - Streetboy 2012-04-04 09:44
What class are you calling this from? Check that NSLog(@"%@", self.navigationController); is not nul - Paul.s 2012-04-04 09:50
Yes it is null. I am calling from myViewControlle - Streetboy 2012-04-04 09:54
It shouldn't be null... your view controller is not inside a navigationControlle - Paul.s 2012-04-04 09:58
Hmm but i am calling from RootViewController and it is null. What may be solution for this - Streetboy 2012-04-04 10:02
When you do self.window.rootViewController = // ... you should be setting it to the navigationController not the rootViewController of the navigationControlle - Paul.s 2012-04-04 10:28
Thank you. That what was wrong : - Streetboy 2012-04-04 10:46


1

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];
2012-04-04 07:59
by Pfitz
I added this to interface:

But when i try to do what you showed i get this: Assigment to read only propert - Streetboy 2012-04-04 08:11

added subclassin - Pfitz 2012-04-04 08:19
I can't do this it fire an error @synthesize navigationBar = _navigationBar;

Property 'navigationBar' attempting to use ivar '_navigationBar' declared in super class 'UINavigationController - Streetboy 2012-04-04 08:38

Won't work on iOS5 - yanchenko 2012-04-24 07:24
@yanchenko Why is that - kevlar 2012-07-22 10:01
Ads