Chlebta
Chlebta

Reputation: 3110

Add buttons to UINavigationController subclass

I'm trying to create reusable subclass of UINavigationController. So here's my code :

    @interface MainNavigationController : UINavigationController
...
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationBar.barTintColor = primaryOrange;
    self.navigationBar.tintColor = white;

    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"search"] style:UIBarButtonItemStylePlain target:self action:nil];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"menu"] style:UIBarButtonItemStylePlain target:self action:nil];


}

So the problem that the color of navigation bar is changing but the buttons they doesn't appears.
What's I've missed here ?

Update I want to have the same NavigationController (button and color ...) for the NavigationController in my image : enter image description here

Update So I've subclasse my navugationController unn this way :

-(void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated{
    viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"search"] style:UIBarButtonItemStylePlain target:self action:nil];
    viewController.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"menu"] style:UIBarButtonItemStylePlain target:self action:@selector(showMainMenu)];

}

- (id)initWithRootViewController:(UIViewController *)rootViewController {
    self = [super initWithRootViewController:rootViewController];
    if (self) {
        rootViewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"search"] style:UIBarButtonItemStylePlain target:self action:nil];
        rootViewController.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"menu"] style:UIBarButtonItemStylePlain target:self action:@selector(showMainMenu)];


    }
    return self;
}

This work's but the only problem is that when I push a view instead of having back button on left I get Always the search icon, how to fix it (I want to have search button for parent on left , and back button for child view ) ?

Upvotes: 2

Views: 2218

Answers (1)

Santu C
Santu C

Reputation: 2654

Subclassing design should be in this way -

- (id)initWithRootViewController:(UIViewController *)rootViewController {
    self = [super initWithRootViewController:rootViewController];
    if (self) {
        [self setLeftButtonToController:rootViewController];
    }
    return self;
}

- (void)setLeftButtonToController:(UIViewController *)viewController {
    UIBarButtonItem *closeItem = [[UIBarButtonItem alloc] initWithTitle:@"Close" style:UIBarButtonItemStylePlain target:self action:@selector(dismissController)];
    [viewController.navigationItem setRightBarButtonItem:closeItem];
}

As requested with full code -

//
//  MyOwnNavigationController.h
// 
//


#import <UIKit/UIKit.h>

@interface MyOwnNavigationController : UINavigationController
- (id)initWithRootViewController:(UIViewController *)rootViewController;
@end



//
//  MyOwnNavigationController.m
// 
//


#import "MyOwnNavigationController.h"

@interface MyOwnNavigationController ()

@end

@implementation MyOwnNavigationController



- (id)initWithRootViewController:(UIViewController *)rootViewController {
    self = [super initWithRootViewController:rootViewController];
    if (self) {
        [self setLeftButtonToController:rootViewController];
    }
    return self;
}

- (void)setLeftButtonToController:(UIViewController *)viewController {
    UIBarButtonItem *closeItem = [[UIBarButtonItem alloc] initWithTitle:@"Close" style:UIBarButtonItemStylePlain target:self action:@selector(dismissController)];
    [viewController.navigationItem setRightBarButtonItem:closeItem];
}


@end

How to use -

TwoViewController * viewController = [[TwoViewController alloc] initWithNibName:@"TwoViewController" bundle:nil];
    MyOwnNavigationController *navController = [[MyOwnNavigationController alloc] initWithRootViewController:viewController];

Update For StoryBroad ---- Add below method to MyOwnNavigationController.m

- (void) loadView 
{
    UIBarButtonItem *closeItem = [[UIBarButtonItem alloc] initWithTitle:@"Close" style:UIBarButtonItemStylePlain target:self action:@selector(dismissController)];
        [self.rootViewController.navigationItem setRightBarButtonItem:closeItem];
}

Upvotes: 2

Related Questions